[GRASS-dev] PYTHONPATH question
Michael Barton
michael.barton at asu.edu
Mon Jul 28 13:13:28 EDT 2008
On Jul 28, 2008, at 5:44 AM, Glynn Clements wrote:
>
> Michael Barton wrote:
>
>>> Is there any pattern to which modules will import and which won't?
>>
>> All the paths in sys.path are valid.
>
> That isn't what I asked.
>
> Which modules will import, and which won't? Presumably "sys" imports
> (that's built into Python)? What about "os"?
>
> Is it just the modules in site-packages, or the "system" modules as
> well?
System modules import fine. It's just site-packages modules
>
>
>> 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
GRASS doc area
GUI description area
most import statements (this is where it stops with an import from
site-packages; the 'system' modules import fine)
def Main(): (i.e., the first procedure called by the parser line)
other procedures
if __name__ == "__main__":
options, flags = grass.parser()
main()
##################################
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
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
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?
Michael
More information about the grass-dev
mailing list