[GRASS-dev] PYTHONPATH question

Glynn Clements glynn at gclements.plus.com
Sun Jul 27 21:53:16 EDT 2008


Michael Barton wrote:

> >>>> I suggest completely unsetting PYTHONPATH, and seeing what problems
> >>> (if any) that causes.
> >>
> >> Interesting. This was something the Mac Python installation puts in,
> >> though I can tell it not to do so. If I unset this, Python works fine
> >> from a terminal and importing in the Python interpreter works fine  
> >> too.
> >>
> >> BUT, there are problems in GRASS because Init.sh now sets $PYTHONPATH
> >> to $GISBASE/etc. This is fine for grass.py, but it causes problems
> >> accessing anything else in site-packages. Here is the result of
> >> running one of the MatPlotLib demo scripts.
> >>
> >> 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 94, in <module>
> >>     import matplotlib
> >> ImportError: No module named matplotlib
> >>
> >> It's looking for matplotlib in $GISBASE/etc now.
> >
> > Any directories in PYTHONPATH should be in addition to the Python
> > system directories, not instead of them.
> >
> > What do you get from:
> >
> > 	import sys
> > 	print '\n'.join(sys.path)
> > ?
> 
> /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/setuptools-0.6c8-py2.5.egg
> /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/Dabo-0.8.3-py2.5.egg
> /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/setuptools-0.6c8-py2.5.egg
> /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/ipython-0.8.4-py2.5.egg
> /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/numpy-1.1.0-py2.5-macosx-10.3-i386.egg
> /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib-0.98.1-py2.5-macosx-10.3.egg
> /Applications/Grass/GRASS-7.0.app/Contents/MacOS/etc/python
> /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages
> /usr/local/lib/wxPython-unicode-2.8.8.1/lib/python2.5/site-packages
> /usr/local/lib/wxPython-unicode-2.8.8.1/lib/python2.5/site-packages/wx-2.8-mac-unicode
> /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-dynload
> /usr/local/lib/wxPython-unicode-2.8.8.1/lib/python2.5
> /Library/Frameworks/Python.framework/Versions/2.5/lib/python25.zip
> /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5
> /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-darwin
> /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac
> /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac/lib-scriptpackages
> /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-tk

Is this for a configuration which works, or which doesn't work?

I note that GRASS' etc/python directory is there.

> > I'm wondering whether python is being invoked with the -S switch,
> > which suppresses the automatic "import site" (which causes
> > site-packages to be added to sys.path, along with any subdirectories
> > referenced by any site-packages/*.pth files).
> 
> I don't think so. It works fine when just calling the python  
> interpreter from the shell. I think the issue is now just down to the  
> fact that Init.sh creates a $PYTHONPATH that has $GISBASE/etc/python.  
> Once this is created, the python side of g.parser only looks for  
> modules to import in $PYTHONPATH. If I just invoke python from the  
> GRASS shell to start the interpreter, there is no problem importing  
> matplotlib or anything else. It's only having parsing import  
> matplotlib when it's in a script. This is an improvement over previous  
> complications. I really appreciate your help.

Is there any pattern to which modules will import and which won't?

I notice that matplotlib is a .egg file. I don't have any of these on
my system (I do have a bunch of .egg-info files, which are just plain
text), probably because Gentoo builds everything from source.

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


More information about the grass-dev mailing list