shebang python26 或 python2.6 中引用的内容
对于 Python 脚本,我需要特定的 Python 版本。现在我安装的 Python 2.6 包含 python26 和 python2.6
我应该将哪一个放入 shebang 中?
选项 1:
#!/usr/bin/env python2.6
选项 2:
#!/usr/bin/env python26
编辑:是的,有一个不使用纯 python 的理由。在我们大学的一些环境中,python 链接到 python2.4,而我的代码使用了相当多的 2.6 功能。
For a Python script I need a specific Python version. Now my installation of Python 2.6 contains both python26 and python2.6
Which one should I put in the shebang?
Option 1:
#!/usr/bin/env python2.6
Option 2:
#!/usr/bin/env python26
EDIT: Yes, there is a reason not to use plain python. In some of our environments in the university python is linked to python2.4 and my code uses quite some 2.6 features.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能总是保证 shebang 将被使用(甚至用户将拥有该版本)。
您实际上不应该完全限制为特定版本。最好至少需要一个给定的版本(如果您的代码可以在 Python 2.6 上运行,为什么它不能在 Python 2.7 上运行?我可能几个月后就不会安装 Python 2.6。
)将坚持使用
/usr/bin/env python
shebang 并动态检测版本。不管你相信与否,执行此操作的“正常”方法是:这将为您提供一个 3 个字符的字符串,例如“2.6”或“2.7”。我只检查第一个字符 = '2'(假设您想阻止 Python 3 运行您的脚本,因为它基本上不兼容)和第三个字符 >= '6'。
编辑:请参阅 Petr 的评论 - 使用
sys.version_info[0:2]
代替(给你一对像(2, 6)
或<代码>(2, 7)。You can't always guarantee that the shebang will be used (or even that the user will have that version).
You shouldn't really limit to a specific version exactly. It's best to require at least a given version (if your code works on Python 2.6, why wouldn't it work on Python 2.7? I might not have Python 2.6 installed in a few months time.)
I would stick with the
/usr/bin/env python
shebang and instead dynamically detect the version. Believe it or not, the "normal" way of doing this is:That will give you a 3-character string such as "2.6" or "2.7". I would just check that the first character = '2' (assuming you want to prevent Python 3 from running your scripts, since it's largely incompatible) and the third character >= '6'.
Edit: See Petr's comment -- use
sys.version_info[0:2]
instead (gives you a pair like(2, 6)
or(2, 7)
.刚刚检查了我的 Linux 系统,只有
python2.6
而不是python26
,所以前者看起来更好。只是为了澄清一下,我会使用条件导入,在我的例子中,我需要
OrderedDict
,它仅是 python 2.7+;Just checked on my Linux system there is only
python2.6
notpython26
so the former looks better.Just to clarify, I would use conditional imports instead, in my case I need
OrderedDict
which is python 2.7+ only;为什么不直接使用
/usr/bin/python
来代替呢?有什么理由不这样做吗?如果您还没有它,您可以使用以下命令创建指向它的链接:
ln -s /usr/bin/python26 /usr/bin/python
如果您升级了您的版本,这可以确保兼容性未来的Python。
Why don't you just use
/usr/bin/python
instead? Is there any reason for not doing that?If you don't have it already, you can create a link to it using this command:
ln -s /usr/bin/python26 /usr/bin/python
This ensures compatibility if you ever upgrade your python in the future.