我应该放#! (shebang)在Python脚本中,它应该采用什么形式?

本文翻译自:Should I put #! (shebang) in Python scripts, and what form should it take?

Should I put the shebang in my Python scripts? 我应该把shebang放到我的Python脚本中吗? In what form? 以什么形式?

#!/usr/bin/env python 

or 要么

#!/usr/local/bin/python

Are these equally portable? 这些同样便携吗? Which form is used most? 最常用哪种形式?

Note: the tornado project uses the shebang. 注意: 龙卷风项目使用shebang。 On the other hand the Django project doesn't. 另一方面, Django项目没有。


#1楼

参考:https://stackoom.com/question/Sz7f/我应该放-shebang-在Python脚本中-它应该采用什么形式


#2楼

Sometimes, if the answer is not very clear (I mean you cannot decide if yes or no), then it does not matter too much, and you can ignore the problem until the answer is clear. 有时,如果答案不是很清楚(我的意思是,你不能决定是或否),那么就没有太大的关系,并且直到答案明确的,你可以忽视的问题。

The #! #! only purpose is for launching the script. 唯一的目的是启动脚本。 Django loads the sources on its own and uses them. Django会自行加载并使用源。 It never needs to decide what interpreter should be used. 不需要决定使用哪种解释器。 This way, the #! 这样, #! actually makes no sense here. 在这里实际上没有任何意义。

Generally, if it is a module and cannot be used as a script, there is no need for using the #! 通常,如果它是一个模块并且不能用作脚本,则无需使用#! . On the other hand, a module source often contains if __name__ == '__main__': ... with at least some trivial testing of the functionality. 另一方面,模块源通常包含if __name__ == '__main__': ... ,并且至少对该功能进行了一些琐碎的测试。 Then the #! 然后是#! makes sense again. 再次有意义。

One good reason for using #! 使用#!一个很好的理由#! is when you use both Python 2 and Python 3 scripts -- they must be interpreted by different versions of Python. 是当您同时使用Python 2和Python 3脚本时-它们必须由不同版本的Python解释。 This way, you have to remember what python must be used when launching the script manually (without the #! inside). 这样,您必须记住手动启动脚本时必须使用什么python (内部没有#! )。 If you have a mixture of such scripts, it is a good idea to use the #! 如果您混合使用了这些脚本,则最好使用#! inside, make them executable, and launch them as executables (chmod ...). 在内部,将其设置为可执行文件,然后将其作为可执行文件启动(chmod ...)。

When using MS-Windows, the #! 使用MS-Windows时, #! had no sense -- until recently. 没有意义-直到最近。 Python 3.3 introduces a Windows Python Launcher (py.exe and pyw.exe) that reads the #! Python 3.3引入了Windows Python启动器(py.exe和pyw.exe),其读取#! line, detects the installed versions of Python, and uses the correct or explicitly wanted version of Python. 行,检测已安装的Python版本,并使用正确或明确需要的Python版本。 As the extension can be associated with a program, you can get similar behaviour in Windows as with execute flag in Unix-based systems. 由于扩展可以与程序相关联,因此在Windows中可以获得与基于Unix的系统中的execute标志类似的行为。


#3楼

If you have more than one version of Python and the script needs to run under a specific version, the she-bang can ensure the right one is used when the script is executed directly, for example: 如果您有多个版本的Python,并且脚本需要在特定版本下运行,那么在直接执行脚本时,she-bang可以确保使用正确的版本,例如:

#!/usr/bin/python2.7

Note the script could still be run via a complete Python command line, or via import, in which case the she-bang is ignored. 请注意,脚本仍然可以通过完整的Python命令行或通过import运行,在这种情况下,she-bang会被忽略。 But for scripts run directly, this is a decent reason to use the she-bang. 但是对于直接运行的脚本,这是使用she-bang的一个不错的理由。

#!/usr/bin/env python is generally the better approach, but this helps with special cases. #!/usr/bin/env python通常是更好的方法,但这在特殊情况下有帮助。

Usually it would be better to establish a Python virtual environment, in which case the generic #!/usr/bin/env python would identify the correct instance of Python for the virtualenv. 通常,最好建立一个Python虚拟环境,在这种情况下,通用的#!/usr/bin/env python将为virtualenv标识正确的Python实例。


#4楼

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可以在不同系统上引用python2python3 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”


#5楼

Should I put the shebang in my Python scripts? 我应该把shebang放到我的Python脚本中吗?

Put a shebang into a Python script to indicate: 将shebang放入Python脚本中以指示:

  • this module can be run as a script 该模块可以作为脚本运行
  • whether it can be run only on python2, python3 or is it Python 2/3 compatible 它只能在python2,python3上运行还是与Python 2/3兼容?
  • on POSIX, it is necessary if you want to run the script directly without invoking python executable explicitly 在POSIX上,如果要直接运行脚本而不显式调用python可执行文件,则很有必要

Are these equally portable? 这些同样便携吗? Which form is used most? 最常用哪种形式?

If you write a shebang manually then always use #!/usr/bin/env python unless you have a specific reason not to use it. 如果您手动编写shebang ,请始终使用#!/usr/bin/env python除非有特殊原因不使用它。 This form is understood even on Windows (Python launcher). 即使在Windows(Python启动器)上也可以理解这种形式。

Note: installed scripts should use a specific python executable eg, /usr/bin/python or /home/me/.virtualenvs/project/bin/python . 注意: 已安装的脚本应使用特定的python可执行文件,例如/usr/bin/python/home/me/.virtualenvs/project/bin/python It is bad if some tool breaks if you activate a virtualenv in your shell. 如果您在Shell中激活virtualenv,如果某些工具损坏了,那就很糟糕。 Luckily, the correct shebang is created automatically in most cases by setuptools or your distribution package tools (on Windows, setuptools can generate wrapper .exe scripts automatically). 幸运的是,在大多数情况下,由setuptools或分发包工具自动创建正确的shebang(在Windows上, setuptools可以自动生成wrapper .exe脚本)。

In other words, if the script is in a source checkout then you will probably see #!/usr/bin/env python . 换句话说,如果脚本在源签出中,那么您可能会看到#!/usr/bin/env python If it is installed then the shebang is a path to a specific python executable such as #!/usr/local/bin/python (NOTE: you should not write the paths from the latter category manually). 如果已安装,则shebang是特定python可执行文件的路径,例如#!/usr/local/bin/python (注意:您不应手动编写来自后一类别的路径)。

To choose whether you should use python , python2 , or python3 in the shebang, see PEP 394 - The "python" Command on Unix-Like Systems : 要选择是否在shebang中使用pythonpython2python3 ,请参阅PEP 394-Unix-like Systems上的“ python”命令

  • ... python should be used in the shebang line only for scripts that are source compatible with both Python 2 and 3. ... python应该仅在shebang行中用于与Python 2和3源兼容的脚本。

  • in preparation for an eventual change in the default version of Python, Python 2 only scripts should either be updated to be source compatible with Python 3 or else to use python2 in the shebang line. 为了准备最终更改默认版本的Python,应该将仅Python 2的脚本更新为与Python 3源兼容,或者在shebang行中使用python2


#6楼

Use first 首先使用

which python

This will give the output as the location where my python interpreter (binary) is present. 这将给出输出作为我的python解释器(二进制)所在的位置。

This output could be any such as 此输出可以是任何这样的

/usr/bin/python

or 要么

/bin/python

Now appropriately select the shebang line and use it. 现在,适当选择shebang行并使用它。

To generalize we can use: 概括地说,我们可以使用:

#!/usr/bin/env

or 要么

#!/bin/env
发布了0 篇原创文章 · 获赞 75 · 访问量 56万+

猜你喜欢

转载自blog.csdn.net/w36680130/article/details/105468319