What does the symbol “#!” mean in Python?

不问归期 提交于 2019-12-08 15:40:05

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!