简体   繁体   English

#!/ usr / bin / python和#!/ usr / bin / env python,哪个支持?

[英]#!/usr/bin/python and #!/usr/bin/env python, which support?

How should the shebang for a Python script look like? Python脚本的shebang应该怎么样?

Some people support #!/usr/bin/env python because it can find the Python interpreter intelligently. 有些人支持#!/usr/bin/env python因为它可以智能地找到Python解释器。 Others support #!/usr/bin/python , because now in most GNU/Linux distributions python is the default program. 其他人支持#!/usr/bin/python ,因为现在在大多数GNU / Linux发行版中, python是默认程序。

What are the benefits of the two variants? 这两种变体有什么好处?

The Debian Python Policy states: Debian Python Policy声明:

The preferred specification for the Python interpreter is /usr/bin/python or /usr/bin/pythonX.Y . Python解释器的首选规范是/usr/bin/python/usr/bin/pythonX.Y This ensures that a Debian installation of python is used and all dependencies on additional python modules are met. 这确保了使用了python的Debian安装,并且满足了对其他python模块的所有依赖性。

Maintainers should not override the Debian Python interpreter using /usr/bin/env python or /usr/bin/env pythonX.Y . 维护者不应使用/usr/bin/env python/usr/bin/env pythonX.Y覆盖Debian Python解释器。 This is not advisable as it bypasses Debian's dependency checking and makes the package vulnerable to incomplete local installations of python. 这是不可取的,因为它绕过Debian的依赖性检查并使包易受python的不完整本地安装的影响。

Note that Debian/Ubuntu use the alternatives system to manage which version /usr/bin/python actually points to. 请注意,Debian / Ubuntu使用替代系统来管理/usr/bin/python实际指向的版本。 This has been working very nicely across a lot of python versions at least for me (and I've been using python from 2.3 to 2.7 now), with excellent transitions across updates. 至少对我来说,这已经在很多python版本中运行得很好(我现在一直在使用2.3到2.7的python),并且在更新过程中有很好的转换。

Note that I've never used pip . 请注意, 我从未使用过pip I want automatic security upgrades, so I install all my python needs via aptitude . 我想要自动安全升级,所以我通过aptitude安装我所有的python需求。 Using the official Debian/Ubuntu packages keep my system much cleaner than me messing around with the python installation myself. 使用官方Debian / Ubuntu软件包让我的系统比我自己搞乱python安装更清洁


Let me emphasize one thing. 让我强调一件事。 The above recommendation refers to system installation of python applications. 上面的建议是指python应用程序的系统安装。 It makes perfectly sense to have these use the system managed version of python. 让这些使用python的系统管理版本是完全合理的。 If you are actually playing around with your own, customized installation of python that is not managed by the operating system, using the env variant probably is the correct way of saying "use the user-preferred python", instead of hard-coding either the system python installation (which would be /usr/bin/python ) or any user-custom path. 如果您实际上正在玩自己的, 不是由操作系统管理的自定义python安装,使用env变体可能说“使用用户首选的python”的正确方式,而不是硬编码系统python安装(可能是/usr/bin/python )或任何用户自定义路径。

Using env python will cause your programs to behave differently if you call them from eg a python virtualenv. 如果从python virtualenv中调用它们,使用env python将导致程序的行为不同。

This can be desired (eg you are writing a script to work only in your virtualenv). 可能是期望的 (例如,您正在编写脚本以在您的virtualenv中工作)。 And it can be problematic (you write a tool for you, and expect it to work the same even within a virtualenv - it may suddenly fail because it is missing packages then). 可能会有问题 (你为你编写一个工具,并期望它甚至在virtualenv中工作相同 - 它可能会突然失败,因为它丢失了包)。

My humble opinion is that you should use the env -variant. 我的拙见是你应该使用env -variant。 It's a POSIX component thus found in pretty much every system, while directly specifying /usr/bin/python breaks in many occasions, ie virtualenv setups. 它是一个POSIX组件,因此几乎在每个系统中都可以找到,而在很多情况下直接指定/usr/bin/python中断,即virtualenv设置。

I use #!/usr/bin/env python as the default install location on OS-X is NOT /usr/bin . 我用#!/usr/bin/env python作为默认的OS-X的安装位置是不是 /usr/bin This also applies to users who like to customize their environment -- /usr/local/bin is another common place where you might find a python distribution. 这也适用于喜欢自定义环境的用户 - /usr/local/bin是另一个可以找到python发行版的常见位置。

That said, it really doesn't matter too much. 也就是说,这真的无关紧要。 You can always test the script with whatever python version you want: /usr/bin/strange/path/python myscript.py . 你总是可以用你想要的任何python版本来测试脚本: /usr/bin/strange/path/python myscript.py Also, when you install a script via setuptools, the shebang seems to get replaced by the sys.executable which installed that script -- I don't know about pip , but I would assume it behaves similarly. 此外,当您通过setuptools安装脚本时,shebang似乎被安装该脚本的sys.executable所取代 - 我不知道pip ,但我认为它的行为类似。

As you note, they probably both work on linux. 如你所知,他们可能都在Linux上工作。 However, if someone has installed a newer version of python for their own use, or some requirement makes people keep a particular version in /usr/bin, the env allows the caller to set up their environment so that a different version will be called through env. 但是,如果某人安装了较新版本的python供自己使用,或者某些要求使人们在/ usr / bin中保留特定版本,则env允许调用者设置其环境,以便通过其调用不同的版本ENV。

Imagine someone trying to see if python 3 works with the scripts. 想象一下有人试图看看python 3是否适用于脚本。 They'll add the python3 interpreter first in their path, but want to keep the default on the system running on 2.x. 他们将首先在路径中添加python3解释器,但希望保持系统上的默认值在2.x上运行。 With a hardcoded path that's not possible. 使用硬编码路径是不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM