Should I put the shebang in my Python scripts? In what form?
#!/usr/bin/env python
or
Sometimes, if the answer is not very clear (I mean you cannot decide if yes or no), then it does not matter too much, and you can ignore the problem until the answer is clear.
The #!
only purpose is for launching the script. Django loads the sources on its own and uses them. It never needs to decide what interpreter should be used. This way, the #!
actually makes no sense here.
Generally, if it is a module and cannot be used as a script, there is no need for using the #!
. On the other hand, a module source often contains if __name__ == '__main__': ...
with at least some trivial testing of the functionality. Then the #!
makes sense again.
One good reason for using #!
is when you use both Python 2 and Python 3 scripts -- they must be interpreted by different versions of Python. This way, you have to remember what python
must be used when launching the script manually (without the #!
inside). If you have a mixture of such scripts, it is a good idea to use the #!
inside, make them executable, and launch them as executables (chmod ...).
When using MS-Windows, the #!
had no sense -- until recently. Python 3.3 introduces a Windows Python Launcher (py.exe and pyw.exe) that reads the #!
line, detects the installed versions of Python, and uses the correct or explicitly wanted version of Python. As the extension can be associated with a program, you can get similar behaviour in Windows as with execute flag in Unix-based systems.