What does "#!/usr/bin/env python" do?

Hallvard B Furuseth (nospam nospam) h.b.furuseth at usit.uio.no
Mon Nov 10 02:20:55 EST 2003


Parzival wrote:

> I am a now immigrant to Linux from (the old country)
> Can someone explain what the line:
> 
> #!/usr/bin/env python
> 
> at the start of a python script does? All my reading of
> bash and env manpages would lead me to believe that
> 
> #!python 
> 
> is equivalent to the first line, but not speaking well the
> language am I, so here the question am I asking.

At least on the operating systems I have checked, #! must be followed by
the full pathname of a program.  It does not search $PATH for it.

'/usr/bin/env python' searches $PATH for python and runs it.
(Usually env is used to set some environment variables for a program,
e.g. 'env PYTHONPATH=whatever python', but one need not specify any
environment variables.)

Since env is (almost?) always in /usr/bin, while python can be installed
God-knows-where, that is more portable than writing #!/local/bin/python
or #!/usr/bin/python or whatever.

-- 
Hallvard




More information about the Python-list mailing list