I am trying PyGRASS on Ubuntu 14.04. I have GRASS 7 installed from the UbuntuGIS PPA:
$ dpkg -l | grep grass
ii grass 7.0.4-1~trusty1 all Geographic Resources Analysis Support System (GRASS GIS)
ii grass-core 7.0.4-1~trusty1 amd64 GRASS GIS core components
ii grass-dev 7.0.4-1~trusty1 amd64 GRASS GIS development files
ii grass-doc 7.0.4-1~trusty1 all GRASS GIS user documentation
ii grass-gui 7.0.4-1~trusty1 amd64 GRASS GIS graphical user interfaces
ii libqgisgrass2.0.1 2.0.1-2build2 amd64 QGIS - shared libraries (libqgisgrass)
ii libqgisgrass2.8.1 1:2.8.1-0+20trusty6 amd64 QGIS - shared grass library
ii libqgisgrass7-2.14.0 1:2.14.0+20trusty-ubuntugis amd64 QGIS - shared grass library
ii libqgisgrass7-2.14.1 1:2.14.1+20trusty-ubuntugis amd64 QGIS - shared grass library
ii libqgisgrass7-2.14.2 1:2.14.2+20trusty-ubuntugis amd64 QGIS - shared grass library
ii libqgisgrass7-2.14.3 1:2.14.3+20trusty-ubuntugis amd64 QGIS - shared grass library
ii qgis-plugin-grass 1:2.14.3+20trusty-ubuntugis amd64 GRASS plugin for QGIS
ii qgis-plugin-grass-common 1:2.14.3+20trusty-ubuntugis all GRASS plugin for QGIS - architecture-independent data
ii qgis-provider-grass 1:2.14.3+20trusty-ubuntugis amd64 GRASS provider for QGIS
I created a small script inspired on Example 4 of the Python and GRASS wiki page:
#!/usr/bin/env python
# Example for pyGRASS usage - vector API
from grass.pygrass.modules.shortcuts import general as g
from grass.pygrass.vector import VectorTopo
from grass.pygrass.modules import Module
vectmap = 'myzipcodes_wake'
g.message("Importing SHAPE file ...")
ogrimport = Module('v.in.ogr')
ogrimport('/home/neteler/gis_data/zipcodes_wake.shp', output=vectmap)
g.message("Assessing vector topology...")
zipcodes = VectorTopo(vectmap)
# Open the map with topology:
zipcodes.open()
# query number of topological features
areas = zipcodes.number_of("areas")
islands = zipcodes.number_of("islands")
print ('Map: <' + vectmap + '> with %d areas and %d islands' % (areas, islands))
dblink = zipcodes.dblinks[0]
print ('DB name:')
print (dblink.database)
table = dblink.table()
print ('Column names:')
print (table.columns.names())
print ('Column types:')
print (table.columns.types())
zipcodes.close()
But its execution fails with an ImportError exception:
$ python3 pygrass-demo4.py
Traceback (most recent call last):
File "pygrass-demo4.py", line 5, in <module>
from grass.pygrass.modules.shortcuts import general as g
File "/usr/lib/grass70/etc/python/grass/pygrass/modules/__init__.py", line 2, in <module>
from grass.pygrass.modules.interface import Module, ParallelModuleQueue
File "/usr/lib/grass70/etc/python/grass/pygrass/modules/interface/__init__.py", line 9, in <module>
from grass.pygrass.modules.interface import module
File "/usr/lib/grass70/etc/python/grass/pygrass/modules/interface/module.py", line 10, in <module>
from grass.script.core import Popen, PIPE
File "/usr/lib/grass70/etc/python/grass/script/__init__.py", line 4, in <module>
from core import *
ImportError: No module named 'core'
Do I need to install any other library to use PyGRASS? Or is there something else wrong?
Update I: According to Gene, PyGRASS must be run in a Python 2 environment. However, it only produces a different exception:
Traceback (most recent call last):
File "/home/desouslu/git/pywps-4-demo/processes/pygrass-demo4.py", line 6, in <module>
from grass.pygrass.vector import VectorTopo
File "/usr/lib/grass70/etc/python/grass/pygrass/vector/__init__.py", line 3, in <module>
import grass.lib.gis as libgis
File "/usr/lib/grass70/etc/python/grass/lib/gis.py", line 23, in <module>
_libs["grass_gis.7.0.4"] = load_library("grass_gis.7.0.4")
File "/usr/lib/grass70/etc/python/grass/lib/ctypes_loader.py", line 57, in load_library
raise ImportError,"%s not found." % libname
ImportError: grass_gis.7.0.4 not found.
Update II: User Gene suggests a problem with Eclipse itself. However, executing the script from the command line produces the same results:
$ python pygrass-demo4.py
Traceback (most recent call last):
File "pygrass-demo4.py", line 6, in <module>
from grass.pygrass.vector import VectorTopo
File "/usr/lib/grass70/etc/python/grass/pygrass/vector/__init__.py", line 3, in <module>
import grass.lib.gis as libgis
File "/usr/lib/grass70/etc/python/grass/lib/gis.py", line 23, in <module>
_libs["grass_gis.7.0.4"] = load_library("grass_gis.7.0.4")
File "/usr/lib/grass70/etc/python/grass/lib/ctypes_loader.py", line 57, in load_library
raise ImportError,"%s not found." % libname
ImportError: grass_gis.7.0.4 not found.
Or alternatively using the Python console:
$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from grass.pygrass.modules.shortcuts import general as g
>>> from grass.pygrass.vector import VectorTopo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/grass70/etc/python/grass/pygrass/vector/__init__.py", line 3, in <module>
import grass.lib.gis as libgis
File "/usr/lib/grass70/etc/python/grass/lib/gis.py", line 23, in <module>
_libs["grass_gis.7.0.4"] = load_library("grass_gis.7.0.4")
File "/usr/lib/grass70/etc/python/grass/lib/ctypes_loader.py", line 57, in load_library
raise ImportError,"%s not found." % libname
ImportError: grass_gis.7.0.4 not found.