Should I put #! (shebang) in Python scripts, and what form should it take?
The shebang line in any script determines the script's ability to be executed like a standalone executable without typing python
beforehand in the terminal or when double clicking it in a file manager (when configured properly). It isn't necessary but generally put there so when someone sees the file opened in an editor, they immediately know what they're looking at. However, which shebang line you use is important.
Correct usage for (defaults to version 3.latest) Python 3 scripts is:
#!/usr/bin/env python3
Correct usage for (defaults to version 2.latest) Python 2 scripts is:
#!/usr/bin/env python2
The following should not be used (except for the rare case that you are writing code which is compatible with both Python 2.x and 3.x):
#!/usr/bin/env python
The reason for these recommendations, given in PEP 394, is that python
can refer either to python2
or python3
on different systems.
Also, do not use:
#!/usr/local/bin/python
"python may be installed at /usr/bin/python or /bin/python in those cases, the above #! will fail."
―"#!/usr/bin/env python" vs "#!/usr/local/bin/python"
It's really just a matter of taste. Adding the shebang means people can invoke the script directly if they want (assuming it's marked as executable); omitting it just means python
has to be invoked manually.
The end result of running the program isn't affected either way; it's just options of the means.
If you have more than one version of Python and the script needs to run under a specific version, the she-bang can ensure the right one is used when the script is executed directly, for example:
#!/usr/bin/python2.7
Note the script could still be run via a complete Python command line, or via import, in which case the she-bang is ignored. But for scripts run directly, this is a decent reason to use the she-bang.
#!/usr/bin/env python
is generally the better approach, but this helps with special cases.
Usually it would be better to establish a Python virtual environment, in which case the generic #!/usr/bin/env python
would identify the correct instance of Python for the virtualenv.
Should I put the shebang in my Python scripts?
Put a shebang into a Python script to indicate:
- this module can be run as a script
- whether it can be run only on python2, python3 or is it Python 2/3 compatible
- on POSIX, it is necessary if you want to run the script directly without invoking
python
executable explicitly
Are these equally portable? Which form is used most?
If you write a shebang manually then always use #!/usr/bin/env python
unless you have a specific reason not to use it. This form is understood even on Windows (Python launcher).
Note: installed scripts should use a specific python executable e.g., /usr/bin/python
or /home/me/.virtualenvs/project/bin/python
. It is bad if some tool breaks if you activate a virtualenv in your shell. Luckily, the correct shebang is created automatically in most cases by setuptools
or your distribution package tools (on Windows, setuptools
can generate wrapper .exe
scripts automatically).
In other words, if the script is in a source checkout then you will probably see #!/usr/bin/env python
. If it is installed then the shebang is a path to a specific python executable such as #!/usr/local/bin/python
(NOTE: you should not write the paths from the latter category manually).
To choose whether you should use python
, python2
, or python3
in the shebang, see PEP 394 - The "python" Command on Unix-Like Systems:
...
python
should be used in the shebang line only for scripts that are source compatible with both Python 2 and 3.in preparation for an eventual change in the default version of Python, Python 2 only scripts should either be updated to be source compatible with Python 3 or else to use
python2
in the shebang line.