Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > #!/usr/bin/env python vs. #!/usr/bin/python

Reply
Thread Tools

#!/usr/bin/env python vs. #!/usr/bin/python

 
 
Yves Dorfsman
Guest
Posts: n/a
 
      05-02-2008
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
 
Reply With Quote
 
 
 
 
Roy Smith
Guest
Posts: n/a
 
      05-02-2008
In article <(E-Mail Removed)>,
Ben Finney <(E-Mail Removed)> wrote:

> 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.


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.
 
Reply With Quote
 
 
 
 
D'Arcy J.M. Cain
Guest
Posts: n/a
 
      05-02-2008
On Fri, 02 May 2008 13:24:01 +1000
Ben Finney <(E-Mail Removed)> wrote:
> 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'.


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 <(E-Mail Removed)> | 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.
 
Reply With Quote
 
Jeroen Ruigrok van der Werven
Guest
Posts: n/a
 
      05-02-2008
-On [20080502 05:26], Ben Finney ((E-Mail Removed)) wrote:
>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.


Simple, some systems are not as peculiar as a lot of Linux boxes which
chug everything into /usr/bin, which is OS territory (as has been decreed
long ago by hier(7)), but rather use /usr/local/bin (all BSD Unix and
derivatives) or /opt or whatever convention a particular operating system
has.

And prone to find the wrong Python, it all depends upon proper $PATH
administration.

As such, your script with #!/usr/bin/python is as bad as an ash shell script
with #!/bin/bash. #!/usr/bin/env python is more cross-OS friendly, there's
more than just Linux you know.

--
Jeroen Ruigrok van der Werven <asmodai(-at-)in-nomine.org> / asmodai
イェルーン ラウフ*ック ヴァン デル ウェルヴェン
http://www.in-nomine.org/ | http://www.rangaku.org/ | GPG: 2EAC625B
I dream of Love as Time runs through my hand...
 
Reply With Quote
 
Tim Roberts
Guest
Posts: n/a
 
      05-02-2008
Yves Dorfsman <(E-Mail Removed)> wrote:
>
>On UNIX, some people use
>#!/usr/bin/env python
>
>While other use
>#!/usr/bin/python
>
>Why is one preferred over the other one ?


The /usr/bin/env solution finds the Python interpreter anywhere on the
PATH, whether it be /usr/bin or /usr/local/bin, or whatever. With
/usr/bin/python, it MUST be in /usr/bin.

Way back when, Python wasn't included in Linux distributions by default, so
it was difficult to predict where it would be. /usr/bin/env, on the other
hand, is well-established at that location.

These days, since Python is nearly ubiquitous, I suspect it is not so
important.
--
Tim Roberts, http://www.velocityreviews.com/forums/(E-Mail Removed)
Providenza & Boekelheide, Inc.
 
Reply With Quote
 
Jeroen Ruigrok van der Werven
Guest
Posts: n/a
 
      05-02-2008
-On [20080502 07:51], Ben Finney ((E-Mail Removed)) wrote:
>To my mind, the Python interpreter installed by a package as
>distributed with the OS *is* OS territory and belongs in /usr/bin/.


That's the difference with a distribution, such as Linux, and full OSes ,
such as BSDs or commercial Unix variants. They prefer to keep a pristine
state for the OS vendor files versus what the user can opt to install
himself, hence the /usr/bin - /usr/local/bin separation. Same for sbin, lib,
and so on. It effectively guarantees you can nuke /usr/local without ill
consequences for your OS.

Different philosophies, but after having spent more than 10+ years on too
many Unix and Unix-like systems I know the importance of platform
portability a bit too much and hardcoding a shebang sequence is not the
solution in general. Using env is the, arguably, best solution available.

--
Jeroen Ruigrok van der Werven <asmodai(-at-)in-nomine.org> / asmodai
イェルーン ラウフ*ック ヴァン デル ウェルヴェン
http://www.in-nomine.org/ | http://www.rangaku.org/ | GPG: 2EAC625B
Felix, qui potuit rerum cognoscere causas...
 
Reply With Quote
 
D'Arcy J.M. Cain
Guest
Posts: n/a
 
      05-02-2008
On Fri, 02 May 2008 15:50:22 +1000
Ben Finney <(E-Mail Removed)> wrote:
> > You have lived a sheltered life. Not every packaging system puts the
> > executible in /usr/bin. Many systems use /usr/local/bin.

>
> They use that for the operating-system-installed default Python
> interpreter? Colour me incredulous.


OK, let me get out my crayons. However, note that I did not say
"operating-system-installed." I said a packaging system puts it
there. In fact, the NetBSD packaging system works on many systems
including Linux and thus is not an operating system packager.

--
D'Arcy J.M. Cain <(E-Mail Removed)> | 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.
 
Reply With Quote
 
Thorsten Kampe
Guest
Posts: n/a
 
      05-02-2008
* Ben Finney (Fri, 02 May 2008 23:30:01 +1000)
> The OP was asking why people prefer on over the other. My answer is
> that I prefer specifying "give me the default OS Python" because
> anything not installed by the OS is to non-standardised for me to
> worry about.
>
> Others may prefer something different, but then they get to wear
> whatever problems occur as a result of that choice. I continue to be
> bemused by that preference, and nothing that I've seen so far in this
> thread illuminates the issue more.


You're missing the point. Apart from the really dubious terms you use
("OS installable package"), using env in the first line has exactly the
effect to use the default path of Python (which is the first entry in
your path) and not some hard-coded path (which might not even exist).

Thorsten
 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      05-02-2008
In article <(E-Mail Removed)>,
Ben Finney <(E-Mail Removed)> wrote:

> Whereas if Python is *not* installed from an OS package, it's up to
> the sys admin to ensure that it works -- not up to my program. So I
> don't see the point in making it work by default, when what I want for
> my program is that it works *with the default Python*, not with some
> non-default installation.


Ben,

Have you ever shipped software to a customer? Imagine the following
conversation:

Customer: "Your product is broken. It says it can't find python, and I
know I have it installed".

Vendor: "Where do you have it installed?"

Customer: "In /opt/bin/python"

Vendor: "Oh, that's your problem, it HAS to be in /usr/bin/python".

Customer: "I can't install it there because <insert whatever silly reason
the customer has>. If you can't make your product work without requiring
me to install python in /usr/bin, I'm afraid I can't buy your product".

Vendor: "No problem sir, I'll be happy to tell our sales folks to stop
bothering you".

If you want to hard-code /usr/bin/python into your application, that's your
decision. If you would like to take on the task of convincing every
sysadmin in the world to do things the way you think they should be done,
have fun.
 
Reply With Quote
 
D'Arcy J.M. Cain
Guest
Posts: n/a
 
      05-02-2008
On Fri, 02 May 2008 23:30:01 +1000
Ben Finney <(E-Mail Removed)> wrote:
> The OP was asking why people prefer on over the other. My answer is
> that I prefer specifying "give me the default OS Python" because
> anything not installed by the OS is to non-standardised for me to
> worry about.


As someone else pointed out, not all the world is Linux. So your
version of Linux (I'm not sure whether it is true for all versions or
not) delivers Python as part of the OS. That is simply not true of the
whole world. Some OS distributions have an adjunct facility for
installing packages but they are not part of the OS. Some systems
don't even have that and people must download packages such as Python
and install them manually. Even on Linux there are people who won't
install binaries and use NetBSD's pkgsrc instead. Clearly that cannot
install into /usr/bin since it is not part of the OS.

Certainly #! /usr/bin/python is fine if you never expect your software
to run outside of your own little corner of the world but you asked why
people prefer the env version and the answer is that we want to write
software that runs everywhere that Python runs.

--
D'Arcy J.M. Cain <(E-Mail Removed)> | 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: [Python-Dev] [python-committers] [RELEASED] Python 3.2 rc 1 R. David Murray Python 0 01-17-2011 02:23 PM
Re: [Python-Dev] [python-committers] [RELEASED] Python 3.2 rc 1 Senthil Kumaran Python 0 01-17-2011 10:31 AM
Re: [Python-Dev] [Python-3000] RELEASED Python 2.6a1 and 3.0a3 Martin v. Lwis Python 0 03-01-2008 10:51 PM
Re: [Python-Dev] [Python-3000] RELEASED Python 2.6a1 and 3.0a3 Paul Moore Python 0 03-01-2008 10:39 PM
Searching comp.lang.python/python-list@python.org (was: UTF-8) skip@pobox.com Python 0 03-10-2007 02:50 PM



Advertisments