[GRASS-dev] PYTHONPATH question

Michael Barton michael.barton at asu.edu
Tue Jul 29 12:22:01 EDT 2008


Glynn,

See below...

On Jul 29, 2008, at 6:36 AM, Glynn Clements wrote:

>
> 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

GRASS 7.0.svn (Spearfish60_test):~ > histogram_mpldemo.py

********* sys.argv ***********

/Applications/Grass/GRASS-7.0.app/Contents/MacOS/scripts/ 
histogram_mpldemo.py

********* sys.path ***********

/Applications/Grass/GRASS-7.0.app/Contents/MacOS/scripts
/Library/Python/2.5/site-packages/GDAL-1.5.2-py2.5-macosx-10.5-i386.egg
/Applications/Grass/GRASS-7.0.app/Contents/MacOS/etc/python
/Users/cmbarton
/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
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/ 
python25.zip
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/ 
plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/ 
plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/ 
plat-mac/lib-scriptpackages
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/ 
python
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/ 
lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/ 
lib-dynload
/Library/Python/2.5/site-packages
/usr/local/lib/wxPython-unicode-2.8.8.1/lib/python2.5
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/ 
python/PyObjC

********* os.environ ***********

_
HISTFILE
GRASS_FONT
GRASS_HTML_BROWSER
TERM_PROGRAM_VERSION
CVS_RSH
GRASS_SH
USER
HOME
PATH
GRASS_GNUPLOT
DISPLAY
TERM_PROGRAM
LANG
GRASS_ADDON_PATH
TERM
Apple_PubSub_Socket_Render
INFOPATH
SHLVL
SECURITYSESSIONID
GRASS_PAGER
HISTSIZE
EDITOR
MANPATH
GISBASE
CVSROOT
TK_LIBRARY
GRASS_PERL
PYTHONPATH
SSH_AUTH_SOCK
GRASS_HTML_BROWSER_MACOSX
TCL_LIBRARY
DYLD_LIBRARY_PATH
PWD
SHELL
GRASS_XTERM
GRASS_ADDON_ETC
GRASS_OS_STARTUP
TMPDIR
PERL5LIB
GRASS_LD_LIBRARY_PATH
GRASS_PROJSHARE
GRASS_PYTHON
__CF_USER_TEXT_ENCODING
GRASS_WISH
GISRC
GRASS_TCLSH
GRASS_VERSION
GRASS_FONT_CAP
LOGNAME
GIS_LOCK
COMMAND_MODE
Traceback (most recent call last):
   File "/Applications/Grass/GRASS-7.0.app/Contents/MacOS/scripts/ 
histogram_mpldemo.py", line 104, in <module>
     import matplotlib
ImportError: No module named matplotlib
GRASS 7.0.svn (Spearfish60_test):~ >


> .
>
>> 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.

Typo in the email. It is usually there.

>
> 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.

Not that I've set anywhere. $PYTHONSTARTUP is not set.

>
>
> 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.

with -S does not have any site-packages; without -S has site-packages.  
See below.

GRASS 7.0.svn (Spearfish60_test):~ > python
Python 2.5.2 (r252:60911, Feb 22 2008, 07:57:53)
[GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
 >>> import sys
 >>> print '\n'.join(sys.path)

/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
/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
/Users/cmbarton
/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/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
/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib- 
dynload
/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site- 
packages
/usr/local/lib/wxPython-unicode-2.8.8.1/lib/python2.5


GRASS 7.0.svn (Spearfish60_test):~ > python -S
Python 2.5.2 (r252:60911, Feb 22 2008, 07:57:53)
[GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin
 >>> import sys
 >>> print '\n'.join(sys.path)

/Applications/Grass/GRASS-7.0.app/Contents/MacOS/etc/python

/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
/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib- 
dynload
 >>>





More information about the grass-dev mailing list