Friday, 6 July 2018

python - Exception using PyGRASS: "No module named 'core'"


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
from grass.pygrass.modules.shortcuts import general as g
File "/usr/lib/grass70/etc/python/grass/pygrass/modules/__init__.py", line 2, in
from grass.pygrass.modules.interface import Module, ParallelModuleQueue
File "/usr/lib/grass70/etc/python/grass/pygrass/modules/interface/__init__.py", line 9, in
from grass.pygrass.modules.interface import module
File "/usr/lib/grass70/etc/python/grass/pygrass/modules/interface/module.py", line 10, in

from grass.script.core import Popen, PIPE
File "/usr/lib/grass70/etc/python/grass/script/__init__.py", line 4, in
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
from grass.pygrass.vector import VectorTopo

File "/usr/lib/grass70/etc/python/grass/pygrass/vector/__init__.py", line 3, in
import grass.lib.gis as libgis
File "/usr/lib/grass70/etc/python/grass/lib/gis.py", line 23, in
_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
from grass.pygrass.vector import VectorTopo
File "/usr/lib/grass70/etc/python/grass/pygrass/vector/__init__.py", line 3, in
import grass.lib.gis as libgis
File "/usr/lib/grass70/etc/python/grass/lib/gis.py", line 23, in
_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 "", line 1, in

File "/usr/lib/grass70/etc/python/grass/pygrass/vector/__init__.py", line 3, in
import grass.lib.gis as libgis
File "/usr/lib/grass70/etc/python/grass/lib/gis.py", line 23, in
_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.

Answer



The Python version of GRASS GIS is 2.7.x and not 3.x


In Python 2.7.11 (Mac OS X)



import grass.script.core 
core.__file__
'/Applications/GRASS-7.0.app/Contents/MacOS/etc/python/grass/script/core.pyc

In Python 3.5.1


 import grass.script.core
Traceback (most recent call last):
File "", line 1, in
File "/Applications/GRASS-7.0.app/Contents/MacOS/etc/python/grass/script/__init__.py", line 4, in
from core import *

ImportError: No module named 'core'

Why ? This is because Python 3 changes the syntax for imports from within a package (look at Changes in import statement python 3, for example)


In the init.py (/.../python/grass/script/init.py)


"""Python interface to launch GRASS GIS modules in scripts
"""
from core import *
....

In Python 3.x this should be



from .core import *

This syntax is also valid for Python 2.7.x


New


I don't understand your question because:



  1. the first thing you need is to run GRASS GIS from outside and you haven't found the solution in your case (grass_gis.7.0.4 not found and GRASS 7 environment setup for Python)

  2. the solution that I have given you is for working in the Python shell or in a IPython/Jupyter notebook, not in Ellipse and it works in my case (with GRASS 7.04 or GRASS 7.1 in Mac OS X or LinuxMint)

  3. If you want to use Ellipe, there are wikis in the GRASS site (Eclipse, Using Eclipse to develop GRASS Python programs) and answers in GIS SE (as Difference between python IDLE and eclipse on accessing GRASS)



Therefore, for me, the main problem is 1). Have you tried the Python script GRASS GIS 7 with existing location ?


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...