faqts : Computers : Programming : Languages : Python : Common Problems : Shell Scripts

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

52 of 61 people (85%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

What is the #!/usr/bin/python line for?
What different forms of "#!" hack are used, and when?

Mar 2nd, 2000 13:26
Steve Holden, Nathan Wallace, Peter Funk


The string following the "#!" tells the system execution library to
treat the script as standard input to the interpreter, which is
assumed to be located there.  This is sometimes called the "exec\hack".
This allows UNIX users to make their Python scripts executable
and call them by name rather than having to explicitly call the 
Python interpreter.
So they can say
    myscript.py
rather than
    python myscript.py
Since MacOS and Windows don't care about this very first line,
this is only interesting to increase portability on unixoid platforms.  
The utility 'env' normally lives in /usr/bin.  This habit seems to
be even older than the invention of the '#!' exec hack.  Env will
look among all the "usual suspect" directories for an executable,
and sets a standard environment up for program execution.
So using '#!/usr/bin/env python' as first line is best available 
solution for portable Python programs today.  But:
Today unix like systems can be divided in two major groups:
  - freely available (FreeBSD, NetBSD, Linux, where the several flavours
    of Linux are the most prominent ones)
  - commercial Unices (AIX, HP-Ux, Sun Solaris, SGI IRIX, Unixware)
Today the freely available Unices have about 90% market share (This is 
hard to estimate, since you may copy them over and over without having 
to buy new licenses) and the commercial Unices are slowly dying out 
one after the other.  Most Linux distributions come with a bunch of
useful open source software and so include a ready to run Python system,
where the Python interpreter can usually be found as /usr/bin/python.
However those commercial Unices have some niches, where professionals
may still earn some Money. ;-)  Unfortunately these systems usually
come with the original Unix utilities, which many professionals
(including me) consider simply as broken.  So if you want to make
e.g. a Solaris system usable for many practical tasks you normally
end up first installing gcc, bash, GNU-find and so on and not to
forget Python under /usr/local/bin.  After that you usally extend
the default search path $PATH to also include /usr/local/bin.
However there may be people out there, who are permitted to install
Python under /usr/local/bin but are *NOT* permitted to change the
default search path to include /usr/local/python (e.g. for some
mysterious security policy).  Under this rare circumstances the second
form '#!/usr/local/bin/python' comes into play.  This would allow
to call for example a CGI-script from a web server process, which 
didn't has Python on its search path.  But this situation should 
be considered as very exotic and rare today.