script portability trick

Erik Max Francis max at alcyone.com
Thu Jan 17 16:03:22 EST 2002


Roy Dragseth wrote:

> I usually start my python scripts with
> 
> #!/usr/bin/env python
> 
> I haven't seen an environment where env wasn't in /usr/bin yet.
> 
> Anyone?

If you install Python manually, it will invariably be in /usr/local/bin.

The /usr/bin/env trick is fairly good practice, but runs into a problem
if there are multiple Python's (named python) installed on the same
system.  For instance, if your default installation puts one in
/usr/bin, and you upgrade to a new version and it gets installed in
/usr/local/bin.  Now which one /usr/bin/env finds will depend on the
PATH setting, which, for instance, will tend to be different in an
interactive shell than in a cron job.  (The best approach is to rename
or delete the python in /usr/bin if you really don't want that one.)

Using #!/usr/local/bin/python isn't too bad, since you can always put a
symlink in /usr/local/bin to where the Python executable really is
should it not be there.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ Laws are silent in time of war.
\__/ Cicero
    Esperanto reference / http://www.alcyone.com/max/lang/esperanto/
 An Esperanto reference for English speakers.



More information about the Python-list mailing list