In PyGRASS I can only import objects from the module modules:

from grass_session import Session
from grass.pygrass.modules import Module, MultiModule, ParallelModuleQueue


If I'm trying to import other PyGRASS modules like vector I am getting an error:

from grass_session import Session
from grass.pygrass.vector import VectorTopo
Traceback (most recent call last):
  File "/Users/jonas.frei/Documents/Python/pygrass01/main.py", line 2, in <module>
    from grass.pygrass.vector import VectorTopo
  File "/Applications/GRASS-8.2.app/Contents/Resources/etc/python/grass/pygrass/vector/__init__.py", line 4, in <module>
    import grass.lib.gis as libgis
  File "/Applications/GRASS-8.2.app/Contents/Resources/etc/python/grass/lib/gis.py", line 30, in <module>
    _libs["grass_gis.8.2"] = load_library("grass_gis.8.2")
  File "/Applications/GRASS-8.2.app/Contents/Resources/etc/python/grass/lib/ctypes_loader.py", line 105, in __call__
    raise ImportError("Could not load %s." % libname)
ImportError: Could not load grass_gis.8.2.

I already tried to fix this error in so many ways:

I'm using the following environment variables:

  • MacOS
    • GRASSBIN=/Applications/GRASS-8.2.app/Contents/Resources/bin/grass
    • LD_LIBRARY_PATH=/Applications/GRASS-8.2.app/Contents/Resources/lib
    • GISBASE=/Applications/GRASS-8.2.app/Contents/Resources
    • PATH="$PATH:/Applications/GRASS-8.2.app/Contents/Resources/bin"
  • Windows
    • PATH=%PATH%;C:\OSGeo4W\bin
    • GRASSBIN=C:\OSGeo4W\bin\grass78
    • LD_LIBRARY_PATH=C:\OSGeo4W\apps\grass\grass78\lib
    • GISBASE=C:\OSGeo4W\apps\grass\grass78

2 Answers 2


Hint: Let me recommend to use GRASS GIS 8 (and no longer 7.8).

So, in a running GRASS GIS 8 session, run in python:

import os
import subprocess
import sys

# define GRASS data settings (adapt to your needs)
grassdata = "/home/mneteler/grassdata/"
location = "nc_spm_08_grass7"
mapset = "user1"

# Python path: we ask GRASS GIS where its Python packages are
    subprocess.check_output(["grass", "--config", "python_path"], text=True).strip()

# Import GRASS Python bindings
import grass.script as gs
import grass.script.setup as gsetup

# Start GRASS Session
session = gsetup.init(grassdata, location, mapset)

At this point we are in a running GRASS GIS session, let's verify.

# show current GRASS GIS settings

Now we may also import further functionality (e.g. VectorTopo of pygrass):

from grass.pygrass.vector import VectorTopo

and can for example retrieve the topology of a vector map (using the North Carolina dataset):

test_vect = VectorTopo("zipcodes_wake")

This should report:

>>> test_vect.num_primitive_of('point')
>>> test_vect.num_primitive_of('line')
>>> test_vect.num_primitive_of('centroid')
>>> test_vect.num_primitive_of('boundary')

You can add the missing GRASS GIS libraries into your LD_LIBRARY_PATH (or on OSX it will be: DYLD_LIBRARY_PATH)

You can add it on top of your script:

import os

os.environ['LD_LIBRARY_PATH'] = '/usr/lib/grass82/lib/'

Then MarkusN example will also work in a standalone script or in a jupyter notebook. see example (assumes grass-session is installed)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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