Use /usr/bin/env python ... why?

Tim Cargile tecargile at hotmail.com
Thu Jan 9 15:44:41 EST 2003


There was a previous thread on this that referenced a python.org FAQ
that I thought was incomplete.  As a casual observer I would
like to submit the following - in the order of most to least efficient
and least to most flexible (perhaps):

Use '#!/usr/bin/python' when:

      The location of 'python' can be or is guaranteed to
      be '/usr/bin' AND one one wants the least execution
      overhead AND does not wish to alter the 'python' runtine
      environment. Or when PATH searching is not possible
      for some reason (restricted environment - cron job).

Use '#!/python' when:

      The location of 'python' cannot be guaranteed to be 'usr/bin'
      (or any other locaton) AND one one is willing to incur the
      overhead of a PATH search AND a PATH search is possible 
      AND does not wish to alter the 'python' runtine environment.
      I have tested this under Cygin with both 'ksh' and 'python' 
      and it works as one would expect.

Use '#!/usr/bin/env python' when:

      The location of 'env' is guaranteed to be '/usr/bin'
      AND when it is desired to alter the environment prior
      to execution of 'python' AND when one is willing to
      incur the overhead (not as minimal as a PATH search)
      of starting 'env' and one cannot guarantee the location
      of 'python' and one is willing to incur the (minimal)
      overhead of a PATH search for 'python'.  Incidentally,
      I could measure the performance hit (in 10ths of seconds)
      of running with the startup overhead of 'env' on my
      900 Mhz laptop under Cygwin.  On a System with 2000 users
      (not even my laptop) it might make a difference).

Use '#!env python' when:

      The location of 'env' cannot be guaranteed AND the
      location of 'python' cannot be guaranteed AND
      it is desired to alter the environment prior
      to execution of 'python' AND one is willing to
      incur the overhead (admittedly minimal) of searching
      the PATH for 'env' AND starting 'env' (not so minimal) 
      and serching the PATH for 'python'.  A lot of 'AND's in this one.

It appears as though the PRIMARY intent of the 'env' utility
is to alter the environment - not force a PATH search for the
executable.  I have never seen a "#!" line that actually altered
the environment.  Has anyone?

Oh well, at least I have not seen these:

 #!env /usr/bin/python

   and

#!/usr/bin/env /usr/bin/python

These works also, but have yet to observe them in the wild. 

I'm wondering why I see so many (like 100%) '#!/usr/bin/env python'
lines ... in the Cygwin release, at least.  I don't believe this
is a religious issue.  It is simple mechanics/logic.  Computers
are only machines ... except for the UNIVAC ... it was God.

Tim




More information about the Python-list mailing list