[GRASS-dev] PYTHONPATH question

Glynn Clements glynn at gclements.plus.com
Tue Jul 29 09:36:58 EDT 2008


Michael Barton wrote:

> >> AFAICT, everything works without $PYTHONPATH in all cases EXCEPT in
> >> a script running under the GRASS parser.
> >
> > So what changes before and after g.parser?
> >
> > In the scripts which I have seen so far, the imports are at the top
> > level, so they will be executed on both occasions. I can't think of
> > any reason why an import would fail when the script is run initially
> > but then work when the script is re-invoked from g.parser.
> >
> > Can you add commands to print sys.argv and sys.path (and, if you can
> > import "os", os.environ) to the top of the script, and see what is
> > changing before and after g.parser.
> 
> I'm not sure I understand. The structure is a 'standard GRASS script'  
> for Python.
> 
> ##################################
> 
> #!/usr/bin/python

At this point, put:

	import sys
	print sys.argv
	print sys.path

	import os
	print os.environ

When you run the script, you should get two lots of output, one from
before g.parser, one from when the script is re-invoked. Unless
g.parser is never being invoked in the first place.

> This works fine if I type the following on at the GRASS command prompt  
> before executing
> 
> PYTHONPATH="/Library/Frameworks/Python.framework/Versions/2.5/lib/ 
> python2.5/site-packages"$PYTHONPATH

There ought to be a colon between the literal and $PYTHONPATH.

> I thought I'd show you how setting PYTHONPATH in Init.sh was causing  
> the trouble, but discovered something that seems a bit odd. I tried  
> unsetting PYTHONPATH and got the same results as before.
> 
> GRASS 7.0.svn (Spearfish60_test):~ > $PYTHONPATH
> bash: /Applications/Grass/GRASS-7.0.app/Contents/MacOS/etc/python:: No  
> such file or directory

To print a variable, use "echo", e.g. "echo $PYTHONPATH". If you just
type "$PYTHONPATH", the shell will try to execute its value as a
command.

> GRASS 7.0.svn (Spearfish60_test):~ > export PYTHONPATH=""
> GRASS 7.0.svn (Spearfish60_test):~ > $PYTHONPATH
> GRASS 7.0.svn (Spearfish60_test):~ >
> GRASS 7.0.svn (Spearfish60_test):~ > histogram_mpldemo.py
> Traceback (most recent call last):
>    File "/Applications/Grass/GRASS-7.0.app/Contents/MacOS/scripts/ 
> histogram_mpldemo.py", line 93, in <module>
>      import matplotlib
> ImportError: No module named matplotlib
> 
> 
> If I start Python from the GRASS command prompt, sys.path is as before  
> and $PYTHONPATH is unset.  So what is setting the search path for  
> Python modules now?

Unless it's started with the -S switch, the Python interpreter
performs an automatic "import site". This module adds the
site-packages directory to sys.path; it also adds each subdirectory
which is named in one fo the site-packages/*.pth files.

	http://docs.python.org/lib/module-site.html

It shoudn't be necessary for PYTHONPATH to be set unless you need to
use modules which aren't installed in Python's lib directory.

Are any of the other PYTHON* environment variables set? If
$PYTHONSTARTUP is set, that could cause an interactive interpreter to
behave differently from a script.

You could try starting python with and without the -S flag, printing
sys.path in each case. It's possible that there's some oddity in your
setup that is causing site.py to misbehave.

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


More information about the grass-dev mailing list