I follow the Working_with_GRASS_without_starting_it_explicitly with GRASS 7.8.8 (2023), but it did not work as intended. The Python line "gs.message" runs, but the line "gs.run_command('g.region', flags='p')" give me this error:
Traceback (most recent call last):
File "C:\Users\gparada\PycharmProjects\grass_proj\main.py", line 53, in <module>
gs.run_command('g.region', flags='p')
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 342, in handle_errors
raise CalledModuleError(module=None, code=code,
grass.exceptions.CalledModuleError: Module run None g.region -p ended with error
Process ended with non-zero return code 3221225785. See errors in the (error) output.
I didn't find useful information online to solve my error. My code is:
import sys
import os
if __name__ == '__main__':
gisbase = r'C:\Program Files\GRASS GIS 7.8'
gisdb = r'C:\GRASS'
python_path = r'C:\Program Files\GRASS GIS 7.8\etc\python'
location = 'epsg_32718'
mapset = 'PERMANENT'
#other routes
bin_path = r'C:\Program Files\GRASS GIS 7.8\bin'
lib_path = 'C:\Program Files\GRASS GIS 7.8\lib'
extrabin_path = r'C:\Program Files\GRASS GIS 7.8\extrabin'
python39_path = r'C:\Program Files\GRASS GIS 7.8\Python39'
script_path = r'C:\Program Files\GRASS GIS 7.8\scripts'
# Append to path
sys.path.append(python_path)
sys.path.append(bin_path)
sys.path.append(lib_path)
sys.path.append(extrabin_path)
sys.path.append(python39_path)
sys.path.append(script_path)
# Set GISBASE environment variable
os.environ['GISBASE'] = gisbase
os.environ['GISDBASE'] = gisdb
print(sys.path)
import grass.script as gs
import grass.script.setup as gsetup
###########
# launch session
rcfile = gsetup.init(gisbase, gisdb, location, mapset)
gs.message('Current GRASS GIS 7 environment:') # WORKS
gs.run_command('g.region', flags='p') # DONT WORK
# delete the rcfile
os.remove(rcfile)