|

shell – Should I put #! (shebang) in Python scripts, and what form should it take?

shell – Should I put #! (shebang) in Python scripts, and what form should it take?

The shebang line in any script determines the scripts 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 isnt necessary but generally put there so when someone sees the file opened in an editor, they immediately know what theyre 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

Its really just a matter of taste. Adding the shebang means people can invoke the script directly if they want (assuming its marked as executable); omitting it just means python has to be invoked manually.

The end result of running the program isnt affected either way; its just options of the means.

shell – Should I put #! (shebang) in Python scripts, and what form should it take?

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.

Leave a Reply

Your email address will not be published. Required fields are marked *