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
    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
    Aug 26, 2020 at 9:08
  • If you have GRASS installed, you should not do pip install grass. See edits above.
    – Micha
    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
    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
    Aug 31, 2020 at 9:16

Here an updated script which uses the grass-session pip package. It shows some GRASS GIS variables and runs a small statistical computation on a LAZ file (simple.laz) with r.in.pdal. The grassdata directory needs to exist.


pip install grass-session

Python script (please adapt the paths to grassdata and simple.laz to your local settings as well as the EPSG code) below, you can set the shell variable GRASSBIN to the name of your GRASS GIS startup script (e.g., grass82, so export GRASSBIN=grass82) before running the Python session:

from grass_session import Session
import grass.script as gs

# hint: do not use ~ as an alias for HOME
with Session(
    # run in PERMANENT mapset after creation of location "test"
    print("grass-session: tests for PROJ, GDAL, PDAL, GRASS GIS")
    print(gs.parse_command("g.gisenv", flags="s"))

    # simple test: just scan the LAZ file

Certainly one can store the script in a file and execute it with python (should be Python >= v3.6).

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.