The shebang line in any script determines the script's ability to be executed like a standalone executable without typing python
beforehand in the terminal or when double clicking it in a file manager (when configured properly).
(任何脚本中的shebang行都决定了该脚本具有像独立可执行文件一样执行的能力,而无需在终端中事先输入python
或在文件管理器中双击它(如果配置正确)。)
It isn't necessary but generally put there so when someone sees the file opened in an editor, they immediately know what they're looking at. (不必要,但通常放在那里,因此当有人看到在编辑器中打开文件时,他们会立即知道他们在看什么。)
However, which shebang line you use IS important. (但是,您使用的家当线是非常重要的。)
Correct usage for Python 3 scripts is:
(Python 3脚本的正确用法是:)
#!/usr/bin/env python3
This defaults to version 3.latest.
(默认为版本3.latest。)
For Python 2.7.latest use python2
in place of python3
. (对于Python 2.7.latest,请使用python2
代替python3
。)
The following should NOT be used (except for the rare case that you are writing code which is compatible with both Python 2.x and 3.x):
(不应使用以下内容(除了极少数情况下,您正在编写与Python 2.x和3.x兼容的代码):)
#!/usr/bin/env python
The reason for these recommendations, given in PEP 394 , is that python
can refer either to python2
or python3
on different systems.
(在PEP 394中给出这些建议的原因是python
可以在不同系统上引用python2
或python3
。)
It currently refers to python2
on most distributions, but that is likely to change at some point. (目前,在大多数发行版中都使用python2
,但这在某些时候可能会改变。)
Also, DO NOT Use:
(另外,请勿使用:)
#!/usr/local/bin/python
"python may be installed at /usr/bin/python or /bin/python in those cases, the above #! will fail."
(“在这种情况下,python可能安装在/ usr / bin / python或/ bin / python上,上面的#!将失败。”)
-- "#!/usr/bin/env python" vs "#!/usr/local/bin/python"
(- “#!/ usr / bin / env python”与“#!/ usr / local / bin / python”)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…