Home > OS >  Python executable to use in hash-bang line?
Python executable to use in hash-bang line?

Time:10-10

On Linux, my Python programs begin with

#!/usr/bin/env python

If my program is actually written in Python 3.x, should I use python or python3 in that line?

IIRC there was a reason to not use python3, but I cannot remember it (or google it).

CodePudding user response:

Using #!/usr/bin/env python3 is the most reliable method.

PEP 0394 suggests system maintainers to install python2 and python3 commands:

We expect Unix-like software distributions (including systems like macOS and Cygwin) to install the python2 command into the default path whenever a version of the Python 2 interpreter is installed, and the same for python3 and the Python 3 interpreter.

When invoked, python2 should run some version of the Python 2 interpreter, and python3 should run some version of the Python 3 interpreter.

Of course this is only a recommendation but it works like that in practice, at least on Linux systems. For example, on Slackware -current:

$ python2 --version
Python 2.7.18
$ python3 --version
Python 3.8.5

And on Fedora 33:

$ python2 --version
Python 2.7.18
$ python3 --version
Python 3.9.1

Furthermore, it doesn't enforce python to always mean Python 2 or Python 3:

Distributors may choose to set the behavior of the python command as follows:

python2,

python3,

not provide python command,

allow python to be configurable by an end user or a system administrator.

And this is where Linux distributions differ. On Slackware current:

$ python --version
Python 2.7.18

On Fedora 33:

$ python --version
Python 3.9.1

That would mean that your script which is written in Python 3 but uses #!/usr/bin/env python would be interpreted by Python 2.7.18 on Slackware and hence would most likely not work correctly. Using python3 in shebang is also good if every try to run your script on an old Linux system that doesn't even have Python 3 installed (really, there are old systems out there that are still used) as it would not even start letting you know immediately that something is wrong.

Furthermore, PEP 0394 moves on to encouraging using virtual environments or containers such as Docker:

Encourage your end users to use a virtual environment. This makes the user's environment more predictable (possibly resulting in fewer issues), and helps avoid disrupting their system.

For scripts that are only expected to be run in an activated virtual environment, shebang lines can be written as #!/usr/bin/env python, as this instructs the script to respect the active virtual environment.

Applications designed exclusively for a specific environment (such as a container or virtual environment) may continue to use the python command name.

But of course in practice it's not going to be easy to make your users always run your script in a virtual environment. Docker containers are easy to use and share and I'm always glad when maintainers public Docker image of their software but if your Python script is small, uses only packages from standard library it may not make sense to use Docker.

  • Related