Why use #!/usr/bin/env python rather than #!python?

Carsten Haese carsten at uniqsys.com
Fri Dec 2 15:24:30 CET 2005


On Fri, 2005-12-02 at 09:12, Adriano Ferreira wrote:
> On 12/2/05, Klaus Alexander Seistrup <klaus at seistrup.dk> wrote:
> > #v+
> >
> > $ ls -l /tmp/hello.py
> > -rwxr-xr-x  1 klaus klaus 38 2005-12-02 14:59 /tmp/hello.py
> > $ cat /tmp/hello.py
> > #! python
> > print 'Hello, world!'
> > # eof
> > $ /tmp/hello.py
> > bash: /tmp/hello.py: python: bad interpreter: No such file or directory
> > $
> >
> > #v-
> 
> Hey, that's not fair. In your illustration above, does 'python' can be
> found in the PATH? That is,
> 
> $ python /tmp/hello.py
> 
> works? If it does, probably
> 
> #!/usr/bin/python
> #!/usr/bin/env python
> #!python
> 
> would also work if
> (1) 'python' is at '/usr/bin/python' (but that's inflexible)
> (2) 'python' can be found in the environment variable path (if 'env'
> is at '/usr/bin/env')
> (3) 'python' can be found in the environment variable path (no need
> for 'env' utility)

(3) assumes that whatever shell the user is running looks up the shebang
executable in the path, which bash, just to name one example, does not
do.

(2) and (1) require that you know where env and python live,
respectively, that's true, but env is likely to be found in an
OS-dependent standard location than python, so (2) is preferable.

HTH,

Carsten.





More information about the Python-list mailing list