newbie q: /usr/bin/env why?

Matthew Dixon Cowles matt at mondoinfo.com
Sat Aug 26 18:48:17 EDT 2000


On Sat, 26 Aug 2000 15:12:20 -0700, Erik Max Francis <max at alcyone.com>
wrote:

>lynx wrote:
>> okay, i've noted the tradition of python scripts invoking
>> themselves with "#!/usr/bin/env python", but i can't figure out
>> why. exactly what good thing does the env(1) invocation accomplish
>> that python finds useful?

>Using /usr/bin/env prevents you from hardcoding the Python
>interpreter's path, which may be different on different systems
>(e.g., /usr/bin/python, /opt/python, /usr/local/bin/python, etc.).
>/usr/bin/env does all that work for you, as ensured to be in a
>standard place.

Erik is of course exactly correct. But there's one additional subtlety
that may be of importance to lynx, whom I recall asked about using
Python for CGI scripting. Putting /usr/bin/env in the #! line will
start the Python interpreter as long as the interpreter is somewhere
in the user's PATH. For interactive use, that's a reasonable
assumption. But in some cases, such as CGI scripts and scripts run
from cron, the "user" generally has a very minimal environment set up
and the Python interpreter may not be in a directory that's listed in
the PATH environment variable. In those circumstances, it's better in
my opinion to hard-code the interpreter's location than to fiddle the
PATH variable.

Regards,
Matt



More information about the Python-list mailing list