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'
## SPECIFY LOCATION AND MAPSET
## 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
sys.exit(-1)
gisbase = out.strip('\n\r')
## SPECIFY GRASS ENVIRONMENT VARIABLES
# 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")
sys.path.append(gpydir)
# Define GRASS DATABASE
# 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
gsetup.init(gisbase,
gisdb, location, mapset)
# say hello
grass.message('--- GRASS GIS 7: Current GRASS GIS 7 environment:')
print grass.gisenv()
# exit
sys.exit(0)