treecoder treecoder - 4 months ago 15
Bash Question

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

Should I put the shebang in my Python scripts? In what form?

#!/usr/bin/env python


or

#!/usr/local/bin/python


Are these equally portable? Which form is used most?

Note: the tornado project uses the shebang. On the other hand the Django project doesn't.

Answer

The shebang line in any script determines the script's ability to be executed like an 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 is:

#!/usr/bin/env python

#!/usr/bin/env python Usually defaults to python 2.7.latest, and the following defaults to 3.latest

#!/usr/bin/env python3

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"