Is there a way to use ArcPy and PyGRASS tools in the same Python script?

928
1
01-14-2013 11:09 PM
MartinScheiber
Occasional Contributor
I´m trying to write a Python script, that includes different ArcPy / ArcGIS commands and want to combine it with GRASS geoprocessing tools.

Unfortunately importing the GRASS libraries doesn´t work.
import grass.script as grass

ends in an error:
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
ImportError: No module named script


I know that GRASS GIS installs it's own Python version. Isn´t there a way to combine the ArcGIS and the GRASS GIS installation of Python? I tried copying the pygrass module (or what I thought it might be) to the "C:\Python26\ArcGIS10.0\Lib\site-packages\" folder. I´m getting a different error message, but it´s still not working.

Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "C:\Python26\ArcGIS10.0\lib\site-packages\grass\script\__init__.py", line 1, in <module>
    from core   import *
  File "C:\Python26\ArcGIS10.0\lib\site-packages\grass\script\core.py", line 38, in <module>
    gettext.install('grasslibs', os.path.join(os.getenv("GISBASE"), 'locale'))
  File "C:\Python26\ArcGIS10.0\lib\ntpath.py", line 96, in join
    assert len(path) > 0
TypeError: object of type 'NoneType' has no len()


In a second step I tried to connect to the .py files from GRASS GIS, but it´s still not working.

import sys
sys.path.append('C:/Program Files (x86)/GRASS GIS 6.4.3RC2/etc/python/grass/script/core.py')
sys.path.append('C:/Program Files (x86)/GRASS GIS 6.4.3RC2/etc/python/grass/script/db.py')
sys.path.append('C:/Program Files (x86)/GRASS GIS 6.4.3RC2/etc/python/grass/script/vector.py')
sys.path.append('C:/Program Files (x86)/GRASS GIS 6.4.3RC2/etc/python/grass/script/raster.py')
sys.path.append('C:/Program Files (x86)/GRASS GIS 6.4.3RC2/etc/python/grass/script/setup.py')
sys.path.append('C:/Program Files (x86)/GRASS GIS 6.4.3RC2/etc/python/grass/script/array.py')
sys.path.append('C:/Program Files (x86)/GRASS GIS 6.4.3RC2/etc/python/grass/script/task.py')

import grass.script as grass 


I´m using ArcGIS 10 and GRASS 6.4.3 on a Windows 7 64 Bit machine. Any help would be highly appreciated.
Tags (2)
0 Kudos
1 Reply
curtvprice
MVP Esteemed Contributor
I´m trying to write a Python script, that includes different ArcPy / ArcGIS commands and want to combine it with GRASS geoprocessing tools.


This will only work if:

1) The versionsof Python are the same (and both 32 or 64)
2) The python libraries in use are the same version

I have been pretty sucessful pulling in my EPD library by simply adding them to the Desktop10.pth file, or adding the paths I need to the PYTHONPATH. sys.path.append should work as well, but it is a more cumbersome way to go.

However, I would imagine there are different flavors of GDAL and other libraries at play here, so I don't know if this is doable.

Perhaps your arcpy script could write out a python script and then execute it using the GRASS python in its native environment through a system command line using the subprocess module. This would be ugly but may be the only way.
0 Kudos