It seems to me like the files run the same without that line.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- How to get the return code of a shell script in lu
- Evil ctypes hack in python
It's recommended way, proposed in documentation:
from http://docs.python.org/py3k/tutorial/interpreter.html#executable-python-scripts
Perhaps your question is in this sense:
If you want to use:
$python myscript.py
You don't need that line at all. The system will call python and then python interpreter will run your script.
But if you intend to use:
$./myscript.py
Calling it directly like a normal program or bash script, you need write that line to specify to the system which program use to run it, (and also make it executable with
chmod 755
)It probably makes sense to emphasize one thing that the most have missed, which may prevent immediate understanding. When you type
python
in terminal you don't normally provide a full path. Instead, the executable is up looked inPATH
environment variable. In turn, when you want to execute a Python program directly,/path/to/app.py
, one must tell the shell what interpreter to use (via the hashbang, what the other contributors are explaining above).Hashbang expects full path to an interpreter. Thus to run your Python program directly you have to provide full path to Python binary which varies significantly, especially considering a use of virtualenv. To address portability the trick with
/usr/bin/env
is used. The latter is originally intended to alter environment in-place and run a command in it. When no alteration is provided it runs the command in current environment, which effectively results in the samePATH
lookup which does the trick.Source from unix stackexchange
You can try this issue using virtualenv
Here is test.py
Create virtual environments
activate each environment then check the differences
That is called the shebang line. As the Wikipedia entry explains:
See also the Unix FAQ entry.
Even on Windows, where the shebang line does not determine the interpreter to be run, you can pass options to the interpreter by specifying them on the shebang line. I find it useful to keep a generic shebang line in one-off scripts (such as the ones I write when answering questions on SO), so I can quickly test them on both Windows and ArchLinux.
The env utility allows you to invoke a command on the path:
Considering the portability issues between
python2
andpython3
, you should always specify either version unless your program is compatible with both.Some distributions are shipping
python
symlinked topython3
for a while now - do not rely onpython
beingpython2
.This is emphasized by PEP 394: