适用于Python脚本的正确Shebang [英] Proper shebang for Python script

查看:188
本文介绍了适用于Python脚本的正确Shebang的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

#!/usr/bin/python

最近,我遇到了这个有关爆炸的声明:

Recently, I've came across this shebang declaration:

#!/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?

推荐答案

#!/usr/bin/env python

具有更高的可移植性,因为通常程序/usr/bin/env可用于在没有完整路径的情况下激活"所需的命令.

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

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

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

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

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

这篇关于适用于Python脚本的正确Shebang的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆