Is "#!/usr/bin/env python" the better shebang line ?

T

Timothy Madden

Hello

Sorry if this has been discussed before, my search did not find it.
My questions is if I should use
#!/usr/bin/env python
as the shebang line in a portable and open python script and if it does
help with portability and usage.

First, can one not find /usr/bin/python in any standard system, at least
as much as /usr/bin/env can be found ?

Then, supposing /usr/bin/env is better than /usr/bin/python and I use
it, is it not the case that many editors and code analysing programs,
that want to know the script language for syntax highlighting, code
completion, tags and source code browsing, etc, will loose their
functionality because of this shebang line ? Is this not a good general
reason to use the old one ?

Now I know env is POSIX standard utility, and python is not, and maybe
that could be reason enough, but are there any other issues involved
with using /usr/bin/env ?

Thank you
Timothy Madden
 
B

Benjamin Kaplan

Hello

Sorry if this has been discussed before, my search did not find it.
My questions is if I should use
 #!/usr/bin/env python
as the shebang line in a portable and open python script and if it does help
with portability and usage.

First, can one not find /usr/bin/python in any standard system, at least as
much as /usr/bin/env can be found ?
Not necessarily. The system python (if it exists) is usually in
/usr/bin but what if there is no system-installed Python or the user
would prefer to use a version they compiled themselves and is
therefore in /usr/local/bin? Or what if the system decides /usr/bin is
just for the default system tools and everything else should be
installed in /opt/? env will always be in the same place, but Python
may not be so using env makes it more portable.
 
T

Timothy Madden

Ned said:
This question came up recently on stackoverflow (alas, will result in
urloverflow):

http://stackoverflow.com/questions/1352922/why-is-usr-bin-env-python-supp
osedly-more-correct-than-just-usr-bin-pyth/1352938#1352938
Ok, thank you.
There is quite a flame since May 2008 archived on mail.python.org about
this issue.

#!/usr/bin/env python is clearly the choice for my scripts.

It helps a lot with portability, and the arguments against it are based
on the idea that /usr/bin/python is somehow better than /opt/python and
that /opt/python is still in front of /usr/bin/python in path. Such an
argument I do not find compelling. The thing is that people writing
large applications for many systems need flexibility first.

Timothy Madden
 
R

ryles

Hello

Sorry if this has been discussed before, my search did not find it.
My questions is if I should use
   #!/usr/bin/env python
as the shebang line in a portable and open python script and if it does
help with portability and usage.

Note that there is one limitation of /usr/bin/env on many systems: you
cannot supply any arguments to the interpreter.

e.g. #!/usr/bin/env python -u

The above will fail on many systems as env will literally try to find
a file named "python -u".

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/237208

Another, more obvious, case where you might avoid /usr/bin/env is when
you know (or might expect in the future) that there is another Python
version on the system which your script will not work with. Then it's
often convenient to hard code the interpreter path. This comes up
mostly in environments where you're not the administrator and have
another Python version installed in a custom place which you (and any
co-developers) have in their PATH. For example, if you wanted to share
your script with a non-team member, then having /usr/bin/env in your
script would force them to have to change their PATH first.
Then, supposing /usr/bin/env is better than /usr/bin/python and I use
it, is it not the case that many editors and code analysing programs,
that want to know the script language for syntax highlighting, code
completion, tags and source code browsing, etc, will loose their
functionality because of this shebang line ? Is this not a good general
reason to use the old one ?

I personally use vim, and it will recognize either shebang, or if none
is present (as in a typical importable module) it will notice the file
suffix, .py. It's not an issue.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,042
Latest member
icassiem

Latest Threads

Top