While this is not a change in Python 3, the transition increased the number of
systems that have more than one Python interpreter installed: it is not
python3.9 to all be valid system commands; other interpreters may be
installed in non-standard locations.
This makes it important to use the correct command for each situation.
The current Python interpreter should be invoked via
Python provides the path of the currently running interpreter as
This variable should be preferred over
python or other hard-coded commands.
For example, rather than:
The assumption that
'python' is correct is only valid in tightly controlled
environments; however, even in those environments
sys.executable is likely
to be correct.
The documentation does include a warning:
If Python is unable to retrieve the real path to its executable,
sys.executablewill be an empty string or
In practice, this does not apply to mainstream platforms.
sys.executable is unusable, then either your platform’s concept of
launching a process via filename is somehow unusual (and in this
case you should know what to do), or there’s an issue in Python itself.
On Unix, executables written in Python must have a shebang line identifying the interpreter. The correct shebang to use will depend on the environment you are targeting and on the version compatibility of the project.
General recommendations for Python shebangs are listed in the For Python script publishers section of PEP 394.