0

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)
1

1 Answer 1

1

I suggest to try the instructions for PyCharm on the GRASS wiki: https://grasswiki.osgeo.org/wiki/Tools_for_Python_programming#PyCharm_IDE

4
  • Hello Anna, i first look that link but i did not find the "python-grass79.bat" or equivalent in my grass gis folder. Commented Oct 13, 2023 at 21:16
  • I guess this should read python-grass78.bat. Can you please see if that file is there? If you used OSGeo4W to install then it might be here: C:\\OSGeo4W64\bin\python-grass78.bat
    – markusN
    Commented Oct 15, 2023 at 20:22
  • 1
    I install the last version of QGIS that comes with grass 8.3.0 and i found the python-grass83.bat in the location that markusN point it, Thanks Anna and @markusN. Commented Oct 16, 2023 at 5:26
  • Wiki page also updated.
    – markusN
    Commented Oct 20, 2023 at 9:41

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.