#!/ usr / bin / env python与#!/ usr / bin / python [英] #!/usr/bin/env python vs. #!/usr/bin/python

查看:102
本文介绍了#!/ usr / bin / env python与#!/ usr / bin / python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在UNIX上,有些人使用

#!/ usr / bin / env python


其他用途

# !/ usr / bin / python


为什么一个优先于另一个?


谢谢。


-

Yves。
http:// www.SollerS.ca

On UNIX, some people use
#!/usr/bin/env python

While other use
#!/usr/bin/python

Why is one preferred over the other one ?

Thanks.

--
Yves.
http://www.SollerS.ca

推荐答案

Yves Dorfsman< yv ** @ zioup.comwrites:
Yves Dorfsman <yv**@zioup.comwrites:

在UNIX上,有些人使用

#!/ usr / bin / env python


其他用途

#!/ usr / bin / python
On UNIX, some people use
#!/usr/bin/env python

While other use
#!/usr/bin/python



你还没有表明你对

含义的区别的理解是的,所以我会为那些可能不知道的人解释。


shebang行(文件的起始行以#!开头)

利用OS内核确定如何根据前几个字节执行文件

文件的s。 shebang行告诉

内核这个文件应该通过传递给另一个命令启动的

a进程来执行。


指定的命令采用完全限定文件路径的形式,

以及程序的零个或一个参数。那个命令然后由内核执行
,并且Python程序文件作为输入传递给结果进程




因此,两者之间的区别在于执行什么命令来解释Python程序。


*"#! / usr / bin / env python"将运行命令/ usr / bin / env python。

''env(1)''手册页说它的目的是在一个程序中运行

修改了环境,但它还具有在当前PATH变量上搜索命令

的效果,并根据首次出现的

执行。


*"#!的/ usr /斌/蟒"将运行命令/ usr / bin / python,其中

当然是由大多数OS

打包系统安装的系统Python实例。该命令运行,结果是Python

解释器。

You haven''t indicated your understanding of what the difference in
meaning is, so I''ll explain it for those who might not know.

The shebang line (the initial line of the file beginning with "#!")
takes advantage of OS kernels that determine how to execute a file
based on the first few bytes of the file. The shebang line tells the
kernel that this file should be executed by passing it as input to
a process started by another command.

The specified command takes the form of a fully-qualified file path,
and zero or one arguments to the program. That command is then
executed by the kernel, and the Python program file is passed as input
to the resulting process.

The difference between the two is thus what command is executed to
interpret the Python program.

* "#! /usr/bin/env python" will run the command "/usr/bin/env python".
The ''env(1)'' manual page says its purpose is to "run a program in a
modified environment", but it also has the effect that the command
is searched on the current PATH variable, and executed based on the
first occurrence.

* "#! /usr/bin/python" will run the command "/usr/bin/python", which
is of course the system Python instance as installed by most OS
packaging systems. That command is run, and the result is the Python
interpreter.


为什么一个优先于另一个?
Why is one preferred over the other one ?



我从未清楚地理解为什么人们想要使用#! / usr / bin / env

python",它很容易从操作系统安装的那个
中找到不同的Python。我有兴趣看看

的回复是什么,以及原因是什么。


一个可能的原因是程序员是试图允许安装了Python的系统,而不是来自运行

系统包的系统。


我更喜欢"#!的/ usr /斌/蟒"因为我希望我的Python程序

默认情况下使用默认的Python运行,并依赖于操作系统的包管理器安装的Python

。在使用shebang线并且实际上具有标准化文件系统

位置的系统

,默认的Python位于''/usr/bin/python''.


-

\任何足够先进的错误都与|

` \特征无法区分。 a ?? Rich Kulawiec |
_o__)|

Ben Finney

I''ve never clearly understood why people want to use "#! /usr/bin/env
python", which is prone to finding a different Python from the one
installed by the operating system. I''d be interested to see what
responses are in favour of it, and what the reasoning is.

One possible reason is that the programmer is attempting to allow for
systems where Python has been installed, but not from an operating
system package.

I much prefer "#! /usr/bin/python" because I want my Python programs
to, by default, be run with the default Python, and depend on Python
being installed by the operating system''s package manager. On systems
that use shebang lines and that actually have standardised filesystem
locations, the default Python is found at ''/usr/bin/python''.

--
\ "Any sufficiently advanced bug is indistinguishable from a |
`\ feature." a??Rich Kulawiec |
_o__) |
Ben Finney


文章< 87 *** *********@benfinney.id.au> ;,

Ben Finney< bi **************** @ benfinney.id .auwrote:
In article <87************@benfinney.id.au>,
Ben Finney <bi****************@benfinney.id.auwrote:

我从未清楚地理解人们为什么要使用#! / usr / bin / env

python",它很容易从操作系统安装的那个
中找到不同的Python。我有兴趣看看

的回复是什么,以及原因是什么。


一个可能的原因是程序员是试图允许安装了Python的系统,而不是来自运行

系统包的系统。
I''ve never clearly understood why people want to use "#! /usr/bin/env
python", which is prone to finding a different Python from the one
installed by the operating system. I''d be interested to see what
responses are in favour of it, and what the reasoning is.

One possible reason is that the programmer is attempting to allow for
systems where Python has been installed, but not from an operating
system package.



你已经准确了。


我目前正在使用Python编写单元测试作为一个构建系统。

我们的许多开发盒都没有在/ usr / bin中安装python(或者可能是

)。即使他们这样做了,我们也可能想在代码的不同分支上使用不同的
版本的Python。


我们已经构建了Python我们的所有平台和存储在我们的

源控制系统中的二进制文件。当您签出特定分支时,您将获得该分支的正确版本的Python。通过让Python脚本

以#!/ usr / bin / env python开头,我可以通过改变环境来选择我想要的Python版本

。 />

然后,当然,我最近遇到了一台机器,其中env安装在

/ opt / gnu / bin而不是/ usr / bin。叹。有时你不会赢。

You''ve got it exactly.

I''m currently using Python to write unit tests as part of a build system.
Many of our development boxes don''t have python installed in /usr/bin (or
perhaps at all). And even if they did, we might want to use a different
version of Python on different branches of the code.

We''ve got Python built for all our platforms and the binaries stored in our
source control system. When you check out a particular branch, you get the
right version of Python for that branch. By having the Python scripts
start with #!/usr/bin/env python, I can select the version of Python I want
just by changing the environment.

Then, of course, I recently ran across a machine where env was installed in
/opt/gnu/bin instead of /usr/bin. Sigh. Sometimes you just can''t win.


周五,2008年5月2日13:24:01 +1000

Ben Finney< bi ****************@benfinney.id.auwrote:
On Fri, 02 May 2008 13:24:01 +1000
Ben Finney <bi****************@benfinney.id.auwrote:

我更喜欢#!的/ usr /斌/蟒"因为我希望我的Python程序

默认情况下使用默认的Python运行,并依赖于操作系统的包管理器安装的Python

。在使用shebang线并且实际上具有标准化文件系统

位置的系统

,默认Python位于''/ usr / bin / python''。
I much prefer "#! /usr/bin/python" because I want my Python programs
to, by default, be run with the default Python, and depend on Python
being installed by the operating system''s package manager. On systems
that use shebang lines and that actually have standardised filesystem
locations, the default Python is found at ''/usr/bin/python''.



你过着庇护的生活。并非每个包装系统都将

可执行文件放在/ usr / bin中。许多系统使用/ usr / local / bin。 NetBSD

使用/ usr / pkg / bin但允许你定义自己的pkg root。

使用/ usr / bin / env允许你的代码在所有这些系统上运行。


-

D''Arcy JM Cain< da *** @ druid.net |民主是三只狼
http://www.druid.net/darcy/ |和一只羊投票

+1 416 425 1212(mod#0082)(eNTP)|什么是晚餐。

You have lived a sheltered life. Not every packaging system puts the
executible in /usr/bin. Many systems use /usr/local/bin. NetBSD
uses /usr/pkg/bin but allows you to define your own pkg root.
Using /usr/bin/env allows your code to run on all these systems.

--
D''Arcy J.M. Cain <da***@druid.net | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what''s for dinner.


这篇关于#!/ usr / bin / env python与#!/ usr / bin / python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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