3

I am trying to use r.horizon GRASS module(version GRASS 7.8) in Python script. But I couldn't able to access the grass modules. Everytime, I am getting the following error.

File "C:\OSGEO4~1\apps\grass\grass78\etc\python\grass\script\core.py",
  line 343, in handle_errors returncode=returncode)

CalledModuleError: Module run None g.gisenv -n ended with error
Process ended with non-zero return code 3221225781. See errors in the (error) output.

The script that I used:

import os
import sys
import subprocess

grass7bin_win = r"C:\OSGeo4W64\bin\grass78.bat"
grass7bin = grass7bin_win

startcmd = grass7bin + ' --config path'

p = subprocess.Popen(startcmd, shell=True, 
                 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()

if p.returncode != 0:
 print (sys.stderr, 'ERROR: %s' % err)
 print (sys.stderr, "ERROR: Cannot find GRASS GIS 7.8 start script (%s)" % startcmd)
 sys.exit(-1)
gisbase = out.strip(b'\n\r')
gisbase = gisbase.decode("utf-8") 

Set GISBASE environment variable

os.environ['GISBASE'] = gisbase 
os.environ['PATH'] += os.pathsep + os.path.join(gisbase, 'extrabin')
home = os.path.expanduser("~")
os.environ['PATH'] += os.pathsep + os.path.join(home, '.grass7', 'addons', 'scripts')
gpydir = os.path.join(gisbase, "etc", "python")
sys.path.append(gpydir)
gisdb = os.path.join(os.path.expanduser("~"), "Documents\grassdata")
os.environ['GISDBASE'] = gisdb

import GRASS Python bindings (see also pygrass)

import grass.script as gscript
import grass.script.setup as gsetup

location = "nc_spm_08"
mapset   = "PERMANENT"
gsetup.init(gisbase,
        gisdb, location, mapset)

gscript.message('Current GRASS GIS 7.8 environment:')

Could anyone help me out with this?

3
  • Can you please edit and add which GRASS GIS version you use? – markusN Jan 5 at 9:51
  • GRASS GIS 7.8 version – bs karthik reddy Jan 5 at 10:20
  • Is it the latest 7.8.5? – markusN Jan 5 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.