[GRASS-dev] PYTHONPATH question

Glynn Clements glynn at gclements.plus.com
Tue Jul 29 23:26:58 EDT 2008


Michael Barton wrote:

> >> So it IS the shebang at the top.
> >
> > Is "python" an alias? In the shell, type "alias python" and
> > "type python".
> 
> anthgradpc7:~ cmbarton$ alias python
> -bash: alias: python: not found
> anthgradpc7:~ cmbarton$ type python
> python is hashed (/Library/Frameworks/Python.framework/Versions/Current/bin/python)

So that will be the first one in the path (although it may be
referenced through a symlink).

As for the "hashed" part, bash maintains a hashtable of name to
pathname mappings, so that it doesn't have to scan $PATH each time. It
discards the mapping if PATH is modified, although you can use "hash
-r" to manually discard the hashtable.

The only time that the hashing is likely to make itself visibile is if
you add an existing program to a directory which appears earlier in
$PATH. In that situation, bash will continue to use the hashed
location even after the same name has appeared in an earlier
directory.

> >> So what to do to make this work?
> >>
> >> #!/usr/bin/env python ????
> >>
> >> This works on my Mac. Does it work for others?
> >
> > The use of /usr/bin/env is a hack to make it look for "python" in
> > $PATH. The #! syntax requires an absolute path, but there's no telling
> > where python will be installled (Python isn't part of POSIX, so you
> > can't rely up on it being installed in /usr/bin).
> >
> > The actual purpose of env is to run commands with a modified
> > environment. The reason it's being used here (without any environment
> > changes) is just because it allows the program to run to be specified
> > as a name rather than a full path, and because /usr/bin/env will
> > always be available.
> 
> So is this OK? If so, we should put this in the WIKI. Is there a  
> better alternative, since we don't know where python is going to be  
> installed?

The /usr/bin/env trick seems to be quite widely used, and there
doesn't appear to be any other way to make a python script which looks
for python in the path.

OTOH, for your particular system, it's probably a good idea to make
/usr/bin/python refer to whichever version has the most-populated
site-packages directory, as that's the most useful version.

-- 
Glynn Clements <glynn at gclements.plus.com>


More information about the grass-dev mailing list