tags:

views:

769

answers:

5

Anyone know this? I've never been able to find an answer.

+3  A: 

It finds 'python' also in /usr/local/bin, ~/bin, /opt/bin, ... or wherever it may hide.

Dirk Eddelbuettel
thanks! it actually caused a problem for me because, for some reason, env didn't' exist.
Kenneth Reitz
That's a bit misleading. It's only going to find the first "python" on your current $PATH and Python instances on OS X can be pretty good at hiding. In particular, the standard framework builds from python.org and others have "bin" subdirectories within the framework where "python" and other executables and scripts are stored. It's very easy to end up with multiple instances which may or may not have symlinks to them in the usual places, like /usr/local/bin et al. Managing $PATH on OS X in this case is not as straightforward as on systems without framework-style builds.
Ned Deily
+2  A: 

You may find this post to be of interest: http://mail.python.org/pipermail/python-list/2008-May/661514.html

This may be a better explanation: http://mail.python.org/pipermail/tutor/2007-June/054816.html

James Black
+16  A: 

If you're prone to installing python in various and interesting places on your PATH (as in $PATH in typical Unix shells, %PATH on typical Windows ones), using /usr/bin/env will accomodate your whim (well, in Unix-like environments at least) while going directly to /usr/bin/python won't. But losing control of what version of Python your scripts run under is no unalloyed bargain... if you look at my code you're more likely to see it start with, e.g., #!/usr/local/bin/python2.5 rather than with an open and accepting #!/usr/bin/env python -- assuming the script is important I like to ensure it's run with the specific version I have tested and developed it with, NOT a semi-random one;-).

Alex Martelli
Of course, you can always use #!/usr/bin/env python2.5 to constrain the set of semi-random choices :=)
Ned Deily
+1 Great explanation, thanks! I hadn't come across that usage of "env" before...now I have something to add to my scripting bag-of-tricks.
Jim Lewis
+1 for justification of NOT using "env python". Made me realize that if I want to use "env python", I'd better be coding for the lowest common denominator, since I'd have no control over the version of python the user has first in the PATH.
glenn jackman
+7  A: 

From wikipedia

"Shebangs specify absolute paths to system executables; this can cause problems on systems which have non-standard file system layouts"

"Often, the program /usr/bin/env can be used to circumvent this limitation"

S.Lott
+2  A: 

it finds the python executable in your environment and uses that. it's more portable because python may not always be in /usr/bin/python. env is always located in /usr/bin.

Charles Ma