After much struggle I have managed to get a GRASS standalone script run outside of GRASS. I can successfully get an output from running some commands, while others give a very unclear and unhelpful error. I think it may be an environment variable that I have not set which means some libraries are not found when others are, but this still doesnt make much sense to me. Here is the code

from grass_session import Session
from grass.script import core as gcore

def test_this(number, CatchmentVectorFile):

    with Session(gisdb="D:\\GRASSdatabase\\", location="test25", create_opts="EPSG:27700") as session:

        # this works  
        # output --> 
        #   GISDBASE=D:\GRASSdatabase\
        #   LOCATION_NAME=test25

        # this works
        gcore.run_command("g.mapsets", flags="l")
        # output -->
        #   Available mapsets:
        #   PERMANENT argh help herewego1 herewego

        # this works  
        gcore.run_command("g.mapset", mapset="herewego1", flags="c")
        # output -->
        #   WARNING: Concurrent mapset locking is not supported on Windows
        #   Mapset switched. Your shell continues to use the history for the old mapset

        # this does not work!
        gcore.run_command("g.region", n=220000, s=80000, e=520000, w=380000, res=5)

The exception output is

Traceback (most recent call last):
  File "D:\\python_code\\main_3.py", line 61, in <module>
  File "D:\\python_code\\main_3.py", line 53, in main
    test_this(17, CatchmentVectorFile)
  File "D:\\python_code\\main_3.py", line 45, in test_this
    gcore.run_command("g.region", n=220000, s=80000, e=520000, w=380000, res=5)
  File "C:\Program Files\GRASS GIS 7.8\etc\python\grass\script\core.py", line 441, in run_command
    return handle_errors(returncode, returncode, args, kwargs)
  File "C:\Program Files\GRASS GIS 7.8\etc\python\grass\script\core.py", line 343, in handle_errors
grass.exceptions.CalledModuleError: Module run None g.region n=220000 s=80000 e=520000 w=380000 res=5 ended with error
Process ended with non-zero return code 3221225785. See errors in the (error) output.

The fact that Module run None is a slight concern but I am not sure thats the problem. Though, the error message really does not help.

I have been following guidance in https://grasswiki.osgeo.org/wiki/Working_with_GRASS_without_starting_it_explicitly.

I have set the three environment variables as well as GISBASE

set GRASSBIN="C:\Program Files\GRASS GIS 7.8\grass78"
set GISBASE="C:\Program Files\GRASS GIS 7.8\"
set GISRC=D:\Users\<username>\AppData\Local\Temp\1\grass7-<username>-5748\gisrc
set PATH=%PATH%;C:\Program Files\GRASS GIS 7.8\Python37\lib\site-packages\pywin32_system32;C:\Program Files\GRASS GIS 7.8\lib;C:\Program Files\GRASS GIS 7.8\bin;D:\Users\<username>\AppData\Roaming\GRASS7\addons\bin;C:\Program Files\GRASS GIS 7.8\extrabin;C:\Program Files\GRASS GIS 7.8\bin;C:\Program Files\GRASS GIS 7.8\Python37;C:\Program Files\GRASS GIS 7.8\scripts;
  • what makes this harder to diagnose is that there is no detail in the error output. I dont know where to find the errors output referenced in the message "Process ended with non-zero return code 3221225785. See errors in the (error) output." If anyone knows where that is that would also help
    – sobmortin
    Commented Jan 4, 2022 at 11:27


Your Answer

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