Python脚本的正确shebang

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17846908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:19:12  来源:igfitidea点击:

Proper shebang for Python script

pythonportabilityshebang

提问by Adam Matan

I'm usually using the following shebang declaration in my Python scripts:

我通常在我的 Python 脚本中使用以下 shebang 声明:

#!/usr/bin/python

Recently, I've came across this shebang declaration:

最近,我遇到了这个shebang声明:

#!/usr/bin/env python

In the script documentation, it was noted that using this form is "more portable".

在脚本文档中,有人指出使用这种形式“更便携”。

What does this declaration mean? How come there's a space in the middle of the path? Does it actually contribute to protability?

这个声明是什么意思?小路中间怎么会有空位?它真的有助于盈利吗?

采纳答案by jh314

#!/usr/bin/env python

is more portable because in general the program /usr/bin/envcan be used to "activate" the desired command without full path.

更便携,因为通常该程序/usr/bin/env可用于在没有完整路径的情况下“激活”所需的命令。

Otherwise, you would have to specify the full path of the Python interpreter, which can vary.

否则,您必须指定 Python 解释器的完整路径,该路径可能会有所不同。

So no matter if the Python interpreter was in /usr/bin/pythonor in /usr/local/bin/pythonor in your home directory, using #!/usr/bin/env pythonwill work.

因此,无论 Python 解释器是在您的主目录/usr/bin/python/usr/local/bin/python还是在您的主目录中, using#!/usr/bin/env python都可以使用。