It seems to me like the files run the same without that line.
|
If you have several versions of Python installed, In Unix, an executable file that's meant to be interpreted can indicate what interpreter to use by having a If you're talking about other platforms, of course, this rule does not apply (but that "shebang line" does no harm, and will help if you ever copy that script to a platform with a Unix base, such as Linux, Mac, etc). |
|||||||||||||||||||||
|
|
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:
|
|||||||||||||||||
|
|
Expanding a bit on the other answers, here's a little example of how your command line scripts can get into trouble by incautious use of
The json module doesn't exist in Python 2.5. One way to guard against that kind of problem is to use the versioned python command names that are typically installed with most Pythons:
If you just need to distinguish between Python 2.x and Python 3.x, recent releases of Python 3 also provide a
|
|||||||||
|
|
In order to run the python script, we need to tell the shell three things:
The shebang The
|
|||||
|
|
Technically, in Python, this is just a comment line. This line is only used if you run the py script from the shell (from the command line). This is know as the "Shebang!", and it is used in various situations, not just with Python scripts. Here, it instructs the shell to start a specific version of Python (to take care of the rest of the file. |
||||
|
|
|
Perhaps your question is in this sense: If you want to use: 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: 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 |
|||||
|
|
The main reason to do this is to make the script portable across operating system environments. For example under mingw, python scripts use :
and under GNU/Linux distribution it is either:
or
and under the best commercial Unix sw/hw system of all (OS/X), it is:
or on FreeBSD:
However all these differences can make the script portable across all by using:
|
|||||||||||||||||
|
|
It probably makes sense to emphasize one thing that the most have missed, which may prevent immediate understanding. When you type 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 |
||||
|
|
|
It's recommended way, proposed in documentation:
from http://docs.python.org/py3k/tutorial/interpreter.html#executable-python-scripts |
|||||
|
|
The When you do on bash:
on Linux, this calls the This line of the kernel gets called on the file passed to
This reads the very first bytes of the file, and compares them to If that is true, then the rest of the line is parsed by the Linux kernel, which makes another exec call with path
and this works for any scripting language that uses And yes, you can make an infinite loop with:
and an executable file at
If the file started with different bytes, then the Finally, you can add your own shebang handlers with the I don't think POSIX specifies shebangs however: https://unix.stackexchange.com/a/346214/32558 |
||||
|
|
|
This is a shell convention that tells the shell which program can execute the script. #!/usr/bin/env python resolves to a path to the Python binary. |
|||
|
|
|
You can try this issue using virtualenv Here is test.py
Create virtual environments
activate each environment then check the differences
|
|||
|
|
If so, then perhaps you're running the Python program on Windows? Windows doesn't use that line—instead, it uses the file-name extension to run the program associated with the file extension. However in 2011, a "Python launcher" was developed which (to some degree) mimics this Linux behaviour for Windows. This is limited just to choosing which Python interpreter is run — e.g. to select between Python 2 and Python 3 on a system where both are installed. The launcher is optionally installed as |
|||||||||
|
|
If you're running your script in a virtual environment, say
Note that the name of the virtual environment is embedded in the path to the Python interpreter. Therefore, hardcoding this path in your script will cause two problems:
Therefore, to add to Jonathan's answer, the ideal shebang is |
||||
|
|
|
Considering the portability issues between Some distributions are shipping This is emphasized by PEP 394:
|
|||
|
|
|
It just specifies what interpreter you want to use. To understand this, create a file through terminal by doing
and do
because python3 doesn't supprt the print operator. Now go ahead and change the first line of your code to:
and it'll work, printing |
||||
|
|
|
It tells the interpreter which version of python to run the program with when you have multiple versions of python. |
|||
|
|
|
this tells the script where is python directory !
|
||||
|
|
protected by Community♦ Mar 13 '15 at 13:00
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
#!/usr/bin/env pythonat the top. – Chakotay Nov 9 '13 at 22:56