I'm trying to use GRASS module in a Python Skript. I already checked this: Is it possible to use GRASS GIS in Python stand alone scripts? and this Using Python script to control GRASS GIS from outside? but it doesn't help with my problem. I did pip install grass-session which worked well. My script is just: import grass_session and when I run it I get the error RuntimeError: Cannot find GRASS GIS start script: 'grass', set the right one using the GRASSBIN environm. variable.

First I tried GRASSBIN="$path\\to\\software\\grass\\grass78 python grass.py" as suggested in https://grasswiki.osgeo.org/wiki/Working_with_GRASS_without_starting_it_explicitly in the block Python: GRASS GIS 7 with an external library: grass-session but I still got the same error.

After checking http://osgeo-org.1560.x6.nabble.com/GRASSBIN-environment-variable-td5413301.html I solved this error adding the following line before the import line: os.environ['GRASSBIN']=r"path\to\software\grass\grass78", although I feel this only should be necessary if my grass version is different from 7.8.

Now when I run these two lines first I'm happy to see that I don't get the error again but after a while I notice that nothing happens. It keeps running without any exit code unless I stop the script manually. I would expect the script to import the module and finish with exit code 0.

I have grass gis 7.8.2. installed with QGIS and Python 3.7 and working on Windows 10 Pro 64 bit.

2 Answers 2


If you have already started GRASS interactively, and created a LOCATION/MAPSET, then try the full code example:from the wiki.

If you do not yet have your GRASS DB setup, and you want to try constructing a temporary location using python, then the next wiki section gives a working example.

Please try those complete examples, then post back if something does not work.


You should not have to do any pip install grass. In the setup script are the lines:

# Set GISBASE environment variable
os.environ['GISBASE'] = gisbase
# define GRASS-Python environment
gpydir = os.path.join(gisbase, "etc", "python")

If you have GRASS installed, that should take care of finding the GRASS python module grass.script. Can you double check that you are getting the paths correct, etc.??

  • Hi Micha, sorry for the delay, I had holiday. I've tried the second option because it works better for me but I noticed that the code is written in python 2 and I had to addapt some parts to python 3. Now the code was able to create a new location but I get the error: import grass.script as grass ModuleNotFoundError: No module named 'grass.script'; 'grass' is not a package
    – anplaceb
    Commented Aug 26, 2020 at 8:46
  • After doing pip install grass, setting the grassbin env manually with os.environ['GRASSBIN'] = r"path" and importing grass_session before the import grass.script as grass line, it trhrows the next error at line grass.message('--- GRASS GIS 7: Current GRASS GIS 7 environment:') The error is very long and I can only copy the last part: grass.exceptions.CalledModuleError: Module run None g.gisenv -n ended with error Process ended with non-zero return code 3221225781. See errors in the (error) output.
    – anplaceb
    Commented Aug 26, 2020 at 9:08
  • If you have GRASS installed, you should not do pip install grass. See edits above.
    – Micha
    Commented Aug 27, 2020 at 8:03
  • I think my paths are corerct gisbase: D:\path\to\QGIS_3.10\apps\grass\grass78 gpydir: D:\path\to\QGIS_3.10\apps\grass\grass78\etc\python with these paths I get the error no module named 'grass.script'; 'grass' is not a package
    – anplaceb
    Commented Aug 31, 2020 at 9:09
  • I get the other error grass.exceptions.CalledModuleError: Module run None g.gisenv -n ended with error Process ended with non-zero return code 3221225781. See errors in the (error) output. by changing 'etc' to 'Python' in the gpydir path, which I did just for trying and I was surprised to see that that way I avoid the import error, but to be honest I'm not sure why
    – anplaceb
    Commented Aug 31, 2020 at 9:16

Edited 6/2024 (dropping "grass-session"):

Starting with GRASS GIS version 8.4+, the "grass-session" pip package is no longer needed, but an internal Python API is now provided.

This example performs a small statistical calculation on a LAZ file (simple.laz) with [r.in.pdal][3].

First set the Python path and start Python:

export PYTHONPATH=$(grass --config python-path)

Next we import the GRASS GIS Python bindings and test r.in.pdal:

import grass.script as gs
import urllib.request

# download a small sample data file (LiDAR points)

# full path to new project
project = "/tmp/grassproject_epsg_25832"
gs.create_project(project, epsg="25832")

# initialize project (formerly: location)
print("GRASS GIS session: tests for PROJ, GDAL, PDAL, GRASS GIS")
print(gs.parse_command("g.gisenv", flags="s"))

# simple test: scan the LAZ file for boundary extent
  • It looks nice, but it is not maintained. Commented Apr 22 at 11:34
  • Good point. I have edited it, dropping the need for "grass-session" pip package.
    – markusN
    Commented Jun 15 at 15:13

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.