Python script header

First, any time you run a script using the interpreter explicitly, as in

$ python ./my_script.py
$ ksh ~/bin/redouble.sh
$ lua5.1 /usr/local/bin/osbf3

the #! line is always ignored. The #! line is a Unix feature of executable scripts only, and you can see it documented in full on the man page for execve(2). There you will find that the word following #! must be the pathname of a valid executable. So

executes whatever python is on the users $PATH. This form is resilient to the Python interpreter being moved around, which makes it somewhat more portable, but it also means that the user can override the standard Python interpreter by putting something ahead of it in $PATH. Depending on your goals, this behavior may or may not be OK.

Next,

deals with the common case that a Python interpreter is installed in /usr/bin. If it’s installed somewhere else, you lose. But this is a good way to ensure you get exactly the version you want or else nothing at all (“fail-stop” behavior), as in

Finally,

works only if there is a python executable in the current directory when the script is run. Not recommended.

Leave a Comment