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
This is a shell convention that tells the shell which program can execute the script.
resolves to a path to the Python binary.
If you have several versions of Python installed,
/usr/bin/env
will ensure the interpreter used is the first one on your environment's$PATH
. The alternative would be to hardcode something like#!/usr/bin/python
; that's ok, but less flexible.In Unix, an executable file that's meant to be interpreted can indicate what interpreter to use by having a
#!
at the start of the first line, followed by the interpreter (and any flags it may need).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).
this tells the script where is python directory !
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.
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 just specifies what interpreter you want to use. To understand this, create a file through terminal by doing
touch test.py
, then type into that file the following:and do
chmod +x test.py
to make your script executable. After this when you do./test.py
you should get an error saying: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
test
to stdout, because python2 supports the print operator. So, now you've learned how to switch between script interpreters.