Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > #!/usr/bin/python or #!/usr/bin/env python?

Reply
Thread Tools

#!/usr/bin/python or #!/usr/bin/env python?

 
 
John Salerno
Guest
Posts: n/a
 
      08-08-2006
I understand the difference, but I'm just curious if anyone has any
strong feelings toward using one over the other? I was reading that a
disadvantage to the more general usage (i.e. env) is that it finds the
first python on the path, and that might not be the proper one to use. I
don't know if that's a real issue most of the time, but it's at least
something to consider.

And is Python found in directories other than /usr/bin often enough to
justify using the more general form?
 
Reply With Quote
 
 
 
 
Steven Bethard
Guest
Posts: n/a
 
      08-08-2006
John Salerno wrote:
> I understand the difference, but I'm just curious if anyone has any
> strong feelings toward using one over the other? I was reading that a
> disadvantage to the more general usage (i.e. env) is that it finds the
> first python on the path, and that might not be the proper one to use.


I don't see how that's any different from when the Python at
/usr/bin/python isn't the right one to use. I know that's true at my
university:

$ /usr/bin/python -V
Python 2.2.3
$ /usr/local/python/bin/python -V
Python 2.4.3

With this setup, the one in /usr/bin is for the OS and is seldom
updated, and the one in /usr/local is for users and is updated frequently.

Sure, if someone doesn't put /usr/local/python/bin on their PATH,
they're not going to get the right Python, but I don't write code to
Python 2.2 anymore, so if I use ``#!/usr/bin/python`` my code will
probably just fail.

Obviously, in my own code, I only use ``#!/usr/bin/env python``.

STeVe
 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
Guest
Posts: n/a
 
      08-08-2006
John Salerno schrieb:
> I understand the difference, but I'm just curious if anyone has any
> strong feelings toward using one over the other?


I use /usr/bin/env if I don't know what the operating system is;
some systems don't have Python in /usr/bin. I use /usr/bin/pythonX.Y
if I want a specific version on a specific operating system (typically
Linux). I use /usr/bin/python when I'm too lazy to think about it
thoroughly.

Regards,
Martin
 
Reply With Quote
 
skip@pobox.com
Guest
Posts: n/a
 
      08-08-2006

John> I understand the difference, but I'm just curious if anyone has
John> any strong feelings toward using one over the other?

#!/usr/bin/python -> never

#!/usr/bin/env python -> always

Strong enough?


John> I was reading that a disadvantage to the more general usage
John> (i.e. env) is that it finds the first python on the path

That's generally the one you want. On most systems /usr/bin/python will be
the system-provided one (e.g. the one that came with your Linux
distribution). You might prefer a Python of more recent vintage though.
That's commonly installed in /usr/local/bin/python and /usr/local/bin is
generally ahead of /usr/bin in PATH.

John> And is Python found in directories other than /usr/bin often
John> enough to justify using the more general form?

On my Mac I install most stuff in ~/local simply so I don't need to be
root and don't disturb the system-provided software. In cases where other
users need to run such tools I generally use /usr/local.

Skip

 
Reply With Quote
 
John Salerno
Guest
Posts: n/a
 
      08-08-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> On most systems /usr/bin/python will be
> the system-provided one (e.g. the one that came with your Linux
> distribution). You might prefer a Python of more recent vintage though.
> That's commonly installed in /usr/local/bin/python and /usr/local/bin is
> generally ahead of /usr/bin in PATH.


Ah, that answered my next question too! I guess I'll stick with the env
method, since I like the more general, abstract stuff over the absolute
anyway.
 
Reply With Quote
 
Erik Max Francis
Guest
Posts: n/a
 
      08-08-2006
John Salerno wrote:

> I understand the difference, but I'm just curious if anyone has any
> strong feelings toward using one over the other? I was reading that a
> disadvantage to the more general usage (i.e. env) is that it finds the
> first python on the path, and that might not be the proper one to use. I
> don't know if that's a real issue most of the time, but it's at least
> something to consider.
>
> And is Python found in directories other than /usr/bin often enough to
> justify using the more general form?


Yes. If Python (or indeed any other well-behaving software) is
installed manually, it will _not_ be installed in /usr/bin. /usr is
where your primary OS/distribution installation goes. Additional
software goes elsewhere, such as /usr/local.

Always prefer to use env over a hardcoded path, because that hardcoded
path will invariably be wrong. (Yes, for those about to nitpick, it's
conceivable that env might be somewhere other than /usr/bin. However,
that is very rare and results in a no-win situations regardless of the
issue of where Python is installed.)

--
Erik Max Francis && (E-Mail Removed) && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Never contend with a man who has nothing to lose.
-- Baltasar Gracian, 1647
 
Reply With Quote
 
Erik Max Francis
Guest
Posts: n/a
 
      08-08-2006
Martin v. L÷wis wrote:

> I use /usr/bin/env if I don't know what the operating system is;
> some systems don't have Python in /usr/bin. I use /usr/bin/pythonX.Y
> if I want a specific version on a specific operating system (typically
> Linux).


Even there, /usr/bin/env pythonX.Y would be a better choice. (Maybe
that's what you meant.)

--
Erik Max Francis && (E-Mail Removed) && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Never contend with a man who has nothing to lose.
-- Baltasar Gracian, 1647
 
Reply With Quote
 
=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
Guest
Posts: n/a
 
      08-08-2006
Erik Max Francis schrieb:
>> I use /usr/bin/env if I don't know what the operating system is;
>> some systems don't have Python in /usr/bin. I use /usr/bin/pythonX.Y
>> if I want a specific version on a specific operating system (typically
>> Linux).

>
> Even there, /usr/bin/env pythonX.Y would be a better choice. (Maybe
> that's what you meant.)


I'm uncertain. When I refer to /usr/bin/python2.3, I know I get the
system vendor's installation. When I use /usr/bin/env python2.3, I
may get something that somebody has build and which may not work
at all, or lack certain add-on packages that the script needs.
It's bad when the script works for some users but fails for others.

Regards,
Martin
 
Reply With Quote
 
Stephan Kuhagen
Guest
Posts: n/a
 
      08-09-2006
> Always prefer to use env over a hardcoded path, because that hardcoded
> path will invariably be wrong. (Yes, for those about to nitpick, it's
> conceivable that env might be somewhere other than /usr/bin. However,
> that is very rare and results in a no-win situations regardless of the
> issue of where Python is installed.)


Don't yell at me for bringing in another language, but I really like the
trick, Tcl does:

> #!/bin/sh
> # The next line starts Tcl \
> exec tclsh "$0" "$@"


This works by the somewhat weird feature of Tcl, that allows comments to be
continued in the next line with "\" at the end of the comment-line. It
looks unfamiliar, but has several advantages, I think. First it's really
VERY unlikely, that there is no /bin/sh (while I found systems with
different places for env), and you can add some other features at or before
the actual call of the interpreter, i.e. finding the right or preferred
version... - This way I coded a complete software-installer, that runs
either as a Tcl/Tk-Script with GUI, or as bash-script, when no Tcl is
available. - I really would like to have something like that for python,
but I did not find a way to achieve that, yet.

Regards
Stephan

 
Reply With Quote
 
=?iso-8859-2?Q?Micha=B3?= Bartoszkiewicz
Guest
Posts: n/a
 
      08-09-2006
On 2006-08-09 at 08:02:03 (+0200), Stephan Kuhagen wrote:
> Don't yell at me for bringing in another language, but I really like the
> trick, Tcl does:
>
> > #!/bin/sh
> > # The next line starts Tcl \
> > exec tclsh "$0" "$@"

>
> This works by the somewhat weird feature of Tcl, that allows comments to be
> continued in the next line with "\" at the end of the comment-line. It
> looks unfamiliar, but has several advantages, I think. First it's really
> VERY unlikely, that there is no /bin/sh (while I found systems with
> different places for env), and you can add some other features at or before
> the actual call of the interpreter, i.e. finding the right or preferred
> version... - This way I coded a complete software-installer, that runs
> either as a Tcl/Tk-Script with GUI, or as bash-script, when no Tcl is
> available. - I really would like to have something like that for python,
> but I did not find a way to achieve that, yet.


You could use:
#!/bin/sh
"""exec" python "$0" "$@"""


--
__ __________________________________________________ __________________
| \/ | Micha│ Bartoszkiewicz <(E-Mail Removed)> | _ )
| |\/| | GG:2298240 | _ \
|_| |_|_For all resources, whatever it is, you need more. [RFC1925]_|___/
 
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




Advertisments