[GRASS-user] Running Python scripts inside GRASS

I'm inside a GRASS session, with my Windows GRASS Command Line and I try to run a python script using GRASS functions:

import os
import grass.script as grass
env = grass.gisenv()
print env
r = grass.read_command("g.list", type='vect')
print r

GRASS 6.4.0svn (YzeronEPSG)> E:\pythoninput\test_simple.py

Output:

Traceback (most recent call last):
  File "E:\pythoninput\test_simple.py", line 2, in <module>
    import grass.script as grass
  File "C:\GRASS-4-SVN\etc\python\grass\script\__init__.py", line 1, in <module>

    from core import *
  File "C:\GRASS-4-SVN\etc\python\grass\script\core.py", line 30, in <module>
    import subprocess
  File "C:\GRASS-4-SVN\Python25\lib\subprocess.py", line 376, in <module>
    import threading
  File "C:\GRASS-4-SVN\Python25\lib\threading.py", line 13, in <module>
    from collections import deque
ImportError: No module named collections

Is this an error in the PYTHONPATH? And where can I define it inside GRASS?

Thanks for any help,

Sonja

Sonja Jankowfsky wrote:

I'm inside a GRASS session, with my Windows GRASS Command Line and I try
to run a python script using GRASS functions:

  File "E:\pythoninput\test_simple.py", line 2, in <module>
    import grass.script as grass
  File "C:\GRASS-4-SVN\etc\python\grass\script\__init__.py", line 1, in
<module>

    from core import *
  File "C:\GRASS-4-SVN\etc\python\grass\script\core.py", line 30, in
<module>
    import subprocess
  File "C:\GRASS-4-SVN\Python25\lib\subprocess.py", line 376, in <module>
    import threading
  File "C:\GRASS-4-SVN\Python25\lib\threading.py", line 13, in <module>
    from collections import deque
ImportError: No module named collections

Is this an error in the PYTHONPATH? And where can I define it inside GRASS?

This indicates either a broken Python installation or broken
configuration. The collections module should be present in 2.5 (it was
added in 2.4).

From your previous message, I note:

PYTHONPATH
C:\GRASS-4-SVN\etc;C:\GRASS-4-SVN\etc\python;C:\GRASS-4-SVN\lib;C:\GRASS-4-SVN\bin;C:\GRASS-4-SVN\extralib;C:\Python26;

This is wrong. PYTHONPATH should be set to:

  C:\GRASS-4-SVN\etc\python

It shouldn't contain any other directories. Python can find modules
which are part of the Python distribution without any configuration;
PYTHONPATH is only needed for the Python modules which are part of
GRASS.

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

So, there is one good thing: I got to run my python program inside GRASS. However, I lost the python GUI. It's good that there is still the Tcl/Tk interface and QGIS :wink:

In detail:

Reducing the PYTHONPATH to C:\GRASS-4-SVN\etc\python changes nothing in the error message.

However, I had a look in my python installations (Python24,Python25,Python26 and GRASS-4-SVN/Python25) for collections.py and this is only included in Python26.

This explains also, why my python program runs under Eclipse, as I put Python26 into the Eclipse Path.

So, I changed the hole content of my C:\GRASS-4-SVN\Python25 directory with the content of the C:\Python26 directory. I kept the name (Python25) of the directory and the Scripts directory and sip.exe.

Now, I can run my Pythonprogramm under the GRASS windows command line, however I can no longer use the Pythonwx GUI. (which does not seem surprising due to the brutal changes I made ;-))

I don't know anything about the differences between Python25 and Python26 and the dependencies for the wxpython GUI, but in my opinion it would be necessary to update the WinGRASS installation with a Python26 version while keeping the compability to the wxpython GUI, in order to be able to run the python scripts inside GRASS.

Does anybody know an easier solution?

Glynn Clements a écrit :

Sonja Jankowfsky wrote:

I'm inside a GRASS session, with my Windows GRASS Command Line and I try to run a python script using GRASS functions:
    
  File "E:\pythoninput\test_simple.py", line 2, in <module>
    import grass.script as grass
  File "C:\GRASS-4-SVN\etc\python\grass\script\__init__.py", line 1, in <module>

    from core import *
  File "C:\GRASS-4-SVN\etc\python\grass\script\core.py", line 30, in <module>
    import subprocess
  File "C:\GRASS-4-SVN\Python25\lib\subprocess.py", line 376, in <module>
    import threading
  File "C:\GRASS-4-SVN\Python25\lib\threading.py", line 13, in <module>
    from collections import deque
ImportError: No module named collections

Is this an error in the PYTHONPATH? And where can I define it inside GRASS?
    
This indicates either a broken Python installation or broken
configuration. The collections module should be present in 2.5 (it was
added in 2.4).

From your previous message, I note:

PYTHONPATH C:\GRASS-4-SVN\etc;C:\GRASS-4-SVN\etc\python;C:\GRASS-4-SVN\lib;C:\GRASS-4-SVN\bin;C:\GRASS-4-SVN\extralib;C:\Python26;

This is wrong. PYTHONPATH should be set to:

  C:\GRASS-4-SVN\etc\python

It shouldn't contain any other directories. Python can find modules
which are part of the Python distribution without any configuration;
PYTHONPATH is only needed for the Python modules which are part of
GRASS.

--
Sonja Jankowfsky
UR Hydrologie-Hydraulique
Cemagref de Lyon
3 bis quai Chauveau CP 220
69336 Lyon Cedex 09
FRANCE
Tel : (+33)4 72 20 86 11 Fax : (+33)4 78 47 78 75

Sonja Jankowfsky wrote:

So, I changed the hole content of my C:\GRASS-4-SVN\Python25 directory
with the content of the C:\Python26 directory. I kept the name
(Python25) of the directory and the Scripts directory and sip.exe.

Now, I can run my Pythonprogramm under the GRASS windows command line,
however I can no longer use the Pythonwx GUI. (which does not seem
surprising due to the brutal changes I made ;-))

I don't know anything about the differences between Python25 and
Python26 and the dependencies for the wxpython GUI, but in my opinion it
would be necessary to update the WinGRASS installation with a Python26
version while keeping the compability to the wxpython GUI, in order to
be able to run the python scripts inside GRASS.

Does anybody know an easier solution?

Delete the copy of Python that's bundled with GRASS, and add the
necessary packages to your system Python. IIRC, you need wxPython,
PyWin32, and NumPy.

If you have multiple versions of Python installed, the "system" Python
is the one associated with the ".py" extension; use "ftype python.file"
from the command prompt to find out which one this is.

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

I uninstalled Python25 and 24 on my machine and reinstalled Python26 + the packages wxPython,

PyWin32, and NumPy.

the ftype python.file in the command line changed to Python26 now. However, when I try to run the python script in GRASS I still get the following error:

File "E:\pythoninput\test_simple.py", line 2, in <module>
     import grass.script as grass
   File "C:\GRASS-4-SVN\etc\python\grass\script\__init__.py", line 1, in <module>
      from core import *
  File "C:\GRASS-4-SVN\etc\python\grass\script\core.py", line 30, in <module>
     import subprocess
   File "C:\GRASS-4-SVN\Python25\lib\subprocess.py", line 376, in <module>
     import threading
   File "C:\GRASS-4-SVN\Python25\lib\threading.py", line 13, in <module>
     from collections import deque
ImportError: No module named collections

I replaced again the content of the dlls, include, Lib, libs and Scripts folder in GRASS-64-SVN/Python25 with those of my Python26 installation.

This results again in the fact, that I can now run my Python programm in the GRASS command line, but I lost my wxPython interface.
I get the following error message in Msys:

C:\GRASS-4-SVN\Python25\lib\site.py:150: Warning: 'with' will become a reserved keyword in Python 2.6
'import site' failed; use -v for traceback
Traceback (most recent call last):
  File "c:/GRASS-4-SVN/etc/wxpython/gis_set.py", line 23, in <module>
    import os
  File "C:\GRASS-4-SVN\Python25\lib\os.py", line 758
    bs = b""
           ^
SyntaxError: invalid syntax
Error in GUI startup. If necessary, please
report this error to the GRASS developers.
Switching to text mode now.

So, the uninstalling of Python25 didn't help anything.
Does anybody has another idea?
Sonja

Glynn Clements a écrit :

Sonja Jankowfsky wrote:

So, I changed the hole content of my C:\GRASS-4-SVN\Python25 directory with the content of the C:\Python26 directory. I kept the name (Python25) of the directory and the Scripts directory and sip.exe.

Now, I can run my Pythonprogramm under the GRASS windows command line, however I can no longer use the Pythonwx GUI. (which does not seem surprising due to the brutal changes I made ;-))

I don't know anything about the differences between Python25 and Python26 and the dependencies for the wxpython GUI, but in my opinion it would be necessary to update the WinGRASS installation with a Python26 version while keeping the compability to the wxpython GUI, in order to be able to run the python scripts inside GRASS.

Does anybody know an easier solution?
    
Delete the copy of Python that's bundled with GRASS, and add the
necessary packages to your system Python. IIRC, you need wxPython,
PyWin32, and NumPy.

If you have multiple versions of Python installed, the "system" Python
is the one associated with the ".py" extension; use "ftype python.file"
from the command prompt to find out which one this is.

--
Sonja Jankowfsky
UR Hydrologie-Hydraulique
Cemagref de Lyon
3 bis quai Chauveau CP 220
69336 Lyon Cedex 09
FRANCE
Tel : (+33)4 72 20 86 11 Fax : (+33)4 78 47 78 75

Sonja Jankowfsky wrote:

I uninstalled Python25 and 24 on my machine and reinstalled Python26 +
the packages wxPython,

PyWin32, and NumPy.

the ftype python.file in the command line changed to Python26 now.
However, when I try to run the python script in GRASS I still get the following error:

File "E:\pythoninput\test_simple.py", line 2, in <module>
     import grass.script as grass
   File "C:\GRASS-4-SVN\etc\python\grass\script\__init__.py", line 1, in
<module>

     from core import *
  File "C:\GRASS-4-SVN\etc\python\grass\script\core.py", line 30, in
<module>
     import subprocess
   File "C:\GRASS-4-SVN\Python25\lib\subprocess.py", line 376, in <module>

So you didn't remove the one version of Python which should have been
removed, i.e. C:\GRASS-4-SVN\Python25.

You should delete this directory, and change any environment variables
which refer to it to refer to the system version of Python (e.g.
C:\Python26 or C:\Program Files\Python26) instead.

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

So, I deleted the C:\GRASS-4-SVN\Python25 directory and I checked that none of my system environment variables refers to this directory. Additionally I changed the PYTHONHOME to C:\Python26 in the grass64svn.bat file and in the C:\GRASS-4-SVN\etc\init.bat file and I added this variable to my system environment variables and in the C:\GRASS-4-SVN\msys\grass70.bat file.

However, I still get the following error, indicating that it doesn't find the os module.
Is there any other place where a wrong PYTHONPATH could be stored??? And is the wxpython GUI really compatible with Python26?
Thanks,
Sonja

Cleaning up temporary files ...
Starting GRASS ...
'import site' failed; use -v for traceback
Traceback (most recent call last):
  File "c:/GRASS-4-SVN/etc/wxpython/gis_set.py", line 23, in <module>
    import os
ImportError: No module named os
Error in GUI startup. If necessary, please
report this error to the GRASS developers.
Switching to text mode now.
Hit RETURN to continue...

Glynn Clements a écrit :

Sonja Jankowfsky wrote:

I uninstalled Python25 and 24 on my machine and reinstalled Python26 + the packages wxPython,

PyWin32, and NumPy.

the ftype python.file in the command line changed to Python26 now. However, when I try to run the python script in GRASS I still get the following error:

File "E:\pythoninput\test_simple.py", line 2, in <module>
     import grass.script as grass
   File "C:\GRASS-4-SVN\etc\python\grass\script\__init__.py", line 1, in <module>

     from core import *
  File "C:\GRASS-4-SVN\etc\python\grass\script\core.py", line 30, in <module>
     import subprocess
   File "C:\GRASS-4-SVN\Python25\lib\subprocess.py", line 376, in <module>
    
So you didn't remove the one version of Python which should have been
removed, i.e. C:\GRASS-4-SVN\Python25.

You should delete this directory, and change any environment variables
which refer to it to refer to the system version of Python (e.g. C:\Python26 or C:\Program Files\Python26) instead.

--
Sonja Jankowfsky
UR Hydrologie-Hydraulique
Cemagref de Lyon
3 bis quai Chauveau CP 220
69336 Lyon Cedex 09
FRANCE
Tel : (+33)4 72 20 86 11 Fax : (+33)4 78 47 78 75

Sonja wrote:

Is there any other place where a wrong PYTHONPATH could be stored???

if you have the grass source code try from the the msys prompt:

grep -ri python25 mswindows/*

wingrass is a bit of a tangle of startup scripts, especially the osgeo4w
build. the above searches through the custom/lesser known wingrass ones.

you can also try a recursive grep in the GRASS-64-SVN\ directory to try
and find and. If doing that I'd add the -I flag to skip searching in
binaries.

Hamish

Hi There,

I am happy running python inside a OSGeo4W grass instalallation.
I start MSYS, start grass, and then ryn python 2.5 which are run
from C:\OSGeo4W\apps\Python25. But I needed to copy the grass/python
library to C:\OSGeo4W\apps\Python25\Lib\site-packages.
Good luck,

milton

2010/3/9 Hamish <hamish_b@yahoo.com>

Sonja wrote:

Is there any other place where a wrong PYTHONPATH could be stored???

if you have the grass source code try from the the msys prompt:

grep -ri python25 mswindows/*

wingrass is a bit of a tangle of startup scripts, especially the osgeo4w
build. the above searches through the custom/lesser known wingrass ones.

you can also try a recursive grep in the GRASS-64-SVN\ directory to try
and find and. If doing that I’d add the -I flag to skip searching in
binaries.

Hamish


grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user