In my opinion, if this line is not the same file to run.
#1st Floor
You can try to use this issue virtualenv
This is test.py
#! /usr/bin/env python
import sys
print(sys.version)
Create a virtual environment
virtualenv test2.6 -p /usr/bin/python2.6
virtualenv test2.7 -p /usr/bin/python2.7
Activation of each environment, and then check the difference
echo $PATH
./test.py
#2nd Floor
He stressed that most people miss a thing may be justified, which may prevent understand immediately. In the terminal type python
, usually do not provide the full path. But PATH
find the executable file environment variable. Conversely, when you want to directly execute Python program /path/to/app.py
, Shell must tell what the interpreter (by hashbang , what other contributors in the above explanation).
Hashbang hope to have a complete interpreter. Therefore, to run Python programs directly, you must provide the full path to Python binary file, the path is very different, especially considering use when virtualenv . In order to address portability, using the /usr/bin/env
skills. The latter was originally intended to change the place and environment in which to run the command. If no changes, it will run the command in the current environment, so as to effectively result in the same PATH
look.
Sources from the unix stackexchange
#3rd floor
That is shebang line. The entry of the Wikipedia:
In the calculation, "shebang" (also called "hashbang", "hashhpling", "bang bang" or "crunchbang") refers to the character "#!." When they are interpreter instructions in the first two characters (the first line of the text file). In Unix-like operating system, the loader presence of these two characters indicates the file is a script, and try to use the rest of the first line of the file specified by the interpreter to execute the script.
See Unix FAQ entry.
Even on Windows, shebang line can not determine interpreter to run, you can also specify options on the shebang line to pass options to the interpreter. I found the general shebang line remains in the one-off scripts (such as a script I wrote in answer to questions SO) is very useful, so I can quickly test it on Windows and ArchLinux.
Use env utility can invoke commands on the path:
The remaining first parameter specifies the name of the program to be called; according to
PATH
a search environment variable. Any remaining arguments are passed to the program as an argument.
#4th floor
If you have multiple versions of Python installed, it /usr/bin/env
will ensure that the interpreter environment of use is the $PATH
first interpreter. Another method is #!/usr/bin/python
something like hard coding; can be, but not very flexible.
In Unix, to be construed executable files can #!
be used to indicate the interpreter #!
in the beginning of the first row, followed by the interpreter (and all flags may be required).
Of course, if you are talking about other platforms, this rule does not apply (but "shebang line" no harm, and if you copy the script to have a platform Unix-based (such as Linux, Mac), will help, etc.).
#5th Floor
This is a convention Shell, Shell tell it which programs can execute scripts.
#!/usr/bin/env python
Python resolves to the path of the binary file.