有什么理由不明确使用/usr/bin/env python2/python3吗? [英] Any reason not to use /usr/bin/env python2/python3 explicitly?
问题描述
我经常在.py
文件的顶部看到这一点:
#!/usr/bin/env python
由于操作系统默认状态为未知状态,我想知道为什么不是这样的:
#!/usr/bin/env python2
#!/usr/bin/env python3
如果大多数操作系统都提供了此版本的符号链接,那会更好吗?
我环顾四周发现 PEP 394 目前,开发人员应该假设python -> python2 -> python2.x
.它还 指出,可以假定存在版本等效项python3
和python2
.那么, not 留给机会并提供额外字符的危害是什么?
如果由于操作系统默认附带python -> python3
(例如Arch linux)而未安装python2
,则在运行脚本或程序时,我会更喜欢此问题:
/usr/bin/env: ‘python2’: No such file or directory
或者,此错误严重得多,特别是对于新用户(python
存在,它是错误的版本):
File "<stdin>", line 1
print 'Hello world'
^
SyntaxError: Missing parentheses in call to 'print'
也就是说,我发现后者更为常见.至少现在我知道一些典型的兼容性错误,足以使自己想到:哦,对.这是python
符号链接."
两个问题问如何指定/验证所需版本:
-
2013年的问题:也许暗示不应该使用
python3
,因为并非所有发行版都附带了它?
如果大多数操作系统都提供了版本化的env
调用,那么我(或python
程序员)不应使用明显的原因吗?仅使用python
似乎可以满足那些没有版本控制命令的少数人的需要,同时又会给绝大多数拥有版本控制的命令造成混乱.
也许答案是以使用版本控制的命令(我从PEP 394获得了该命令),但是还没有足够的时间看到它们的出现.到目前为止,我还从未见过版本化的env
调用……然后再说一次,如果可行,我再也看不到.如果它中断了,那么它总是没有版本的python
行,因此我的思维可能偏斜了!
有关搜索的github统计信息;我对用法很好奇:
-
#!/usr/bin/env python2
:命中了〜210k python文件代码 -
#!/usr/bin/env python3
:〜460k -
#!/usr/bin/env python
:约600万
这可能意味着大多数代码已经足够老,如果上述问题的建议是普遍的智慧,人们只是没有更新他们的文件?
我查看了这些流行的操作系统,发现它们都使用版本控制的命令:
编写脚本时,可能会导致对特定版本的Python的各种依赖.使用字典理解?您至少需要Python 2.7.使用async
关键字?您至少需要Python 3.5.
使用#!/usr/bin/env python
时,并不是说要执行脚本需要使用哪个版本的Python .您只是在说首先使用在您的路径中找到的任何版本的Python".从根本上说,开发者是错误指定shebang的人.
相反,用户应该将shebang设置为其系统上正确版本的Python的路径.
distutils
软件包在这里是一个很好的折衷方案.您,开发人员,只需在脚本中输入#!python
即可.通过python setup.py install
安装脚本时,安装程序会用目标计算机上正确的路径替换#!python
.
I often see this at the top of .py
files:
#!/usr/bin/env python
With the unknown state of the OS defaults, I wonder why it's not:
#!/usr/bin/env python2
#!/usr/bin/env python3
If most OSs provide this versioned symlink, wouldn't that be better?
I found PEP 394 while looking around, which states that for the time being, developers should assume python -> python2 -> python2.x
. It also states that one can assume that the versioned equivalents, python3
and python2
, exist. So what's the harm in not leaving it to chance and providing that extra character?
If one doesn't have python2
installed because the OS ships by default with python -> python3
(like Arch linux), I'd much prefer this issue when running a script or program:
/usr/bin/env: ‘python2’: No such file or directory
Alternatively, this error is much worse, particularly for a new user (python
exists, it's just the wrong version):
File "<stdin>", line 1
print 'Hello world'
^
SyntaxError: Missing parentheses in call to 'print'
That said, I find the latter far more common. At least now I know some typical compatibility errors well enough to think to myself "Oh, right. It's the python
symlink."
Two questions ask how to specify/verify the desired version:
This 2010 question suggests treating
python
as meaningpython2
andpython3
as required to explicitly call outpython3
.This 2013 question: maybe implies that
python3
shouldn't be used because not all distros ship with it?
Is there an obvious reason I (or python
programmers) shouldn't use a versioned env
call if most OSs provide it? To use just python
is seeming to cater to a minority who don't have the versioned command while causing confusion to the vast majority who do.
Maybe the answer is to use versioned commands (I'm getting that from PEP 394) but enough time hasn't elapsed to see them appear. To date I've never seen a versioned env
call... then again, if it works I never look. If it breaks, it's always a version-less python
line so my mental counts are probably skewed!
Some github stats on searches; I was curious on usage:
#!/usr/bin/env python2
: ~210k python file code hits#!/usr/bin/env python3
: ~460k#!/usr/bin/env python
: ~6 million
This could mean that most code is old enough that if the recommendations of the questions above was the prevailing wisdom, folks just haven't updated their files?
I looked at these popular OSs and found them all to use versioned commands:
- ubuntu: python-minimal and python3-minimal
- arch: python and python2
- fedora: python2 and python3
- debian: same names/state as ubuntu
- various sources suggest that OS X uses versioned commands
- windows: I think there's evidence that windows has versioned commands available
When you write the script, you may induce various dependencies on a particular version of Python. Use a dict comprehension? You need at least Python 2.7. Using the async
keyword? You need at least Python 3.5.
When you use #!/usr/bin/env python
, you aren't saying anything about which version of Python is required to execute your script; you are just saying "Use whatever version of Python is found in your path first." Fundamentally, the developer is the wrong person to specify the shebang.
Instead, the user should be setting the shebang to the path of the correct version of Python on their system.
The distutils
package strikes a good compromise here. You, the developer, just put #!python
in your script. When you install the script via python setup.py install
, the installer replaces #!python
with whatever path is correct on the target machine.
这篇关于有什么理由不明确使用/usr/bin/env python2/python3吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!