I am running GRASS 7.3 on Mac OS 10.11.5 El Capitan. I'm trying to figure out how to get GRASS GIS running via python scripting (pygrass). I'd like to be able to access raster data programatically. For this I want to use either RasterRow or RasterSegment classes from pygrass.

When I try from grass.pygrass import raster it fails with the following back trace:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/user/Desktop/Mac_pyGRASS.py", line 52, in <module>
    from grass.pygrass import raster
  File "/Applications/GRASS-7.3.app/Contents/MacOS/etc/python/grass/pygrass/raster/__init__.py", line 14, in <module>
    import grass.lib.gis as libgis
  File "/Applications/GRASS-7.3.app/Contents/MacOS/etc/python/grass/lib/gis.py", line 23, in <module>
    _libs["grass_gis.7.3.svn"] = load_library("grass_gis.7.3.svn")
  File "/Applications/GRASS-7.3.app/Contents/MacOS/etc/python/grass/lib/ctypes_loader.py", line 64, in load_library
    raise ImportError("%s not found." % libname)
ImportError: grass_gis.7.3.svn not found.

I am able to import other pygrass modules without error.

The relevant portion of my script is as follows:

#!/usr/bin/env python
import os, sys
import subprocess

# path to the GRASS GIS launch script (MacOS)
grass7bin = '/Applications/GRASS-7.3.app/Contents/MacOS/grass73'

## both location and mapset must already exist
location = 'mylocation'
mapset = 'PERMANENT'

# query GRASS 7 for location of GISBASE
startcmd = [grass7bin, '--config', 'path']

p = subprocess.Popen(startcmd, shell=False,
                    stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
if p.returncode != 0:
   print >>sys.stderr, "ERROR: Cannot find GRASS GIS 7 start script (%s)" % startcmd
gisbase = out.strip('\n\r')

# Set GISBASE environment variable
os.environ['GISBASE'] = gisbase ##  = '/Applications/GRASS-7.3.app/Contents/MacOS'
# the following not needed with trunk
os.environ['PATH'] += os.pathsep + os.path.join(gisbase, 'extrabin')
# add path to GRASS addons
home = os.path.expanduser("~")
os.environ['PATH'] += os.pathsep + os.path.join(home, '.grass7', 'addons', 'scripts')

# define GRASS-Python environment
gpydir = os.path.join(gisbase, "etc", "python")

# add your path to grassdata (GRASS GIS database) directory
gisdb = os.path.join(os.path.expanduser("~"), "grassdata")
# Set GISDBASE environment variable
os.environ['GISDBASE'] = gisdb

# Import GRASS Python bindings (see also pygrass)
import grass
import grass.script as gscript
import grass.script.setup as gsetup
import grass.pygrass
from grass.pygrass import raster

from grass.pygrass.modules.shortcuts import general as g
from grass.pygrass.modules.shortcuts import raster as r

## launch session
           gisdb, location, mapset)

# say hello
grass.message('--- GRASS GIS 7: Current GRASS GIS 7 environment:')
print grass.gisenv()

# exit
  • Possible duplicate of this question here? I'm still struggling to figure this out but it seems to be an issue with setting up the GRASS GIS environment properly – corvus Jun 15 '17 at 23:26

This issue has to do with setting the GRASS environment variables properly when trying to use pyGRASS without starting GRASS explicitly (i.e. running a standalone python script that is executed from the command line, rather than being run directly from the python console within GRASS.app).

Specifically, the problem seems to lie in loading the C dynamic libraries using C types. To get around this hangup, python needs to be able to find the GRASS lib folder, i.e. $(GISBASE)/lib (thanks to user wenzeslaus on StackOverflow for this!)

I was able to fix this by running export LD_LIBRARY_PATH=/Applications/GRASS-7.3.app/Content/MacOS/lib in the terminal before executing my python script. Though I haven't yet verified it, I suppose I should be able to make this fix 'permanent' by adding this path to ~/.bash_profile.

(Note that 'GRASS-7.3' should be replaced by your appropriate GRASS version number, and that older versions of GRASS.app are installed to /Applications/GRASS/ rather than /Applications/)

If someone has a better or more elegant solution, or can offer further insights into this issue, I will gladly accept their answer.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.