加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-16 23:08:13 所属栏目:Python 来源:网络整理
导读:Python脚本的样子应该如何? 有些人支持#!/usr/bin/env python,因为它可以智能地找到Python解释器.其他支持#!/usr/bin/python,因为现在在大多数GNU / Linux发行版中,python是默认程序. 这两种变体有什么好处? 解决方法 Debian Python Policy说: The pref
Python脚本的样子应该如何?

有些人支持#!/usr/bin/env python,因为它可以智能地找到Python解释器.其他支持#!/usr/bin/python,因为现在在大多数GNU / Linux发行版中,python是默认程序.

这两种变体有什么好处?

解决方法

Debian Python Policy说:

The preferred specification for the Python interpreter is /usr/bin/python or /usr/bin/pythonX.Y. This ensures that a Debian installation of python is used and all dependencies on additional python modules are met.

Maintainers should not override the Debian Python interpreter using /usr/bin/env python or /usr/bin/env pythonX.Y. This is not advisable as it bypasses Debian’s dependency checking and makes the package vulnerable to incomplete local installations of python.

请注意,Debian / Ubuntu使用替代系统来管理/usr/bin/python实际指向的版本.至少对我来说,这已经非常好地运行在很多python版本中(而且我现在已经在2.3到2.7之间使用python),在更新过程中有很好的转换.

请注意,我从来没有使用过.我想要自动安全升级,所以我通过aptitude安装所有的python需求.使用官方的Debian / Ubuntu软件包可以让我的系统比我自己安装python安装更加清洁.

让我强调一件事以上建议是指系统安装python应用程序.让这些使用系统管理的python版本是非常有意义的.如果您实际使用自己的,自定义安装的python不受操作系统管理,使用env变体可能是说“使用用户喜欢的python”的正确方法,而不是硬编码系统python安装(这将是/usr/bin/python)或任何用户自定义路径.

使用env python会导致你的程序的行为不一样,如果你从一个python virtualenv.

这可以是必需的(例如,您正在编写一个脚本,只在您的virtualenv中工作).这可能是有问题的(你为你写一个工具,并期望它甚至在一个virtualenv中工作 – 它可能突然失败,因为它丢失了包).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读