问题
What does this line of code mean? Without it, my python3 http server can't understand and let the browser download an empty .py file (depend on the link to the .py file)
#! /usr/local/bin/python3
回答1:
It's not a Python thing, it a hashbang (or shebang) line which indicates which interpreter should process the file.
The rules vary but, in its simplest form, a file with the name xyz
(containing that as the first line), when run from the command line with xyz
, will run it using that interpreter, similar to:
/usr/local/bin/python3 xyz
回答2:
This is not a python specific notion, see http://en.wikipedia.org/wiki/Shebang_(Unix)
回答3:
It's the shebang/hashbang line and a Linux/UNIX thing, not Python-related at all.
When executing the file, the kernel will see the #!
magic and use whatever comes after it to execute the script. The actual program that gets launched by the kernel will be program-from-shebang script-file-path [script-args]
Note that it's usually not a good thing to include a .../local/...
path but rather use e.g. #!/usr/bin/env python3
which will result in python3
being looked up in the current PATH which is much more portable.
回答4:
That is not python-specific but is called Shebang and tells the operating system with which program to run this script.
回答5:
UNIX Shebang? See http://en.wikipedia.org/wiki/Shebang_(Unix)
. The space between !
and the first /
probably shouldn't be there.
来源:https://stackoverflow.com/questions/6288855/what-does-the-symbol-mean-in-python