5

Using the tutorials posted here, here and here, I am trying to setup a script that will make use of several GRASS GIS raster functions externally. For now I am just trying to get the tutorial script to run but I receive the following error:

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

When searching for this error it seems that others have issues trying to import grass.script. However, I am able to import this without issue. It is only afterwards when trying to run a module that I get this error. This happens when I try to use a 'gscript.run_command('g.gisenv') (see code block below).

I run: - Windows 10 x64 - Python 2.7.13 x86 - GRASS GIS 7.2.0 x86 - x86 version of GDAL

I removed all other GRASS GIS installations (one was installed with QGIS) and reinstalled it with the required Microsoft Visual C++ Redistributable Packages.

import os
import sys
import subprocess

grass7bin = r'C:\Program Files (x86)\GRASS GIS 7.2.0\grass72.bat'
startcmd = [grass7bin, '--config', 'path']
try:
    p = subprocess.Popen(startcmd, shell=False,
                         stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    out, err = p.communicate()
except OSError as error:
    sys.exit("ERROR: Cannot find GRASS GIS start script"
             " {cmd}: {error}".format(cmd=startcmd[0], error=error))
if p.returncode != 0:
    sys.exit("ERROR: Issues running GRASS GIS start script"
             " {cmd}: {error}"
             .format(cmd=' '.join(startcmd), error=err))
gisbase = out.strip(os.linesep)

os.environ['GISBASE'] = gisbase
grass_pydir = os.path.join(gisbase, "etc", "python")
sys.path.append(grass_pydir)

import grass.script.setup as gsetup

gisdb = r'C:\data\grass'
location = "Country"
mapset = "Topic"

rcfile = gsetup.init(gisbase, gisdb, location, mapset)

import grass.script as gscript
print'grass.script imported'

gscript.run_command('g.gisenv', 'n')

os.remove(rcfile)

EDIT:

After trying to reinstall GRASS GIS several times I decided to download a new GRASS GIS installation file and install that. Now the script runs without throwing the error.

EDIT 2:

I thought the issue was resolved but that is not the case. When I try to use a command as per the tutorial here:

gscript.run_command('r.in.gdal', input = inputVRT, output = outputName, overwrite = True) 

It doesn't work and I again get a CalledModuleError:

Traceback (most recent call last):
  File "C:\scripts\10_1_grassGis.py", line 54, in <module>
    overwrite = True)
  File "C:\Program Files (x86)\GRASS GIS 7.2.0\etc\python\grass\script\core.py", line 410, in run_command
    return handle_errors(returncode, returncode, args, kwargs)
  File "C:\Program Files (x86)\GRASS GIS 7.2.0\etc\python\grass\script\core.py", line 329, in handle_errors
    returncode=returncode)
CalledModuleError: Module run None ['r.in.gdal', '--o', '--q', '-o', 'input=C:\\data\\rasters\\bykerneNet_03743.vrt', 'output=bykerne03743'] ended with error
Process ended with non-zero return code -1073741512. See errors in the (error) output.

However, if I try things like:

gscript.gisenv()

It runs fine.

What could be the issue?

markusN
  • 12,976
  • 31
  • 48
Tins
  • 506
  • 5
  • 16

2 Answers2

2

Things have gotten much easier with GRASS GIS 7.4: there is now "grass-session" to call GRASS GIS functionality from Python.

To install the stable version use:

pip install grass-session

For the usage, see the GRASS GIS Wiki or this example GRASS-GIS-Python session.

markusN
  • 12,976
  • 31
  • 48
0

Obviously, what you did to initiate the GRASS session from inside Python is correct. So simpler module g.gisenv and g.list should work. I can think of the following possibilities to try as this issue can be specific to installation:

  1. Try to see if the virtual raster is the issue. Use a real raster file (not a virtual raster) instead.
  2. If that is not the issue, reinstall GRASS in a folder without space in the folder name like: 'C:\GIS\GRASS_GIS_72\'
  3. Try defining the file path of your raster with os.path.join as resolved on this post by Micha
  4. Some similar modules were resolved in my system by uninstalling xerces-c package and all its dependencies (including GDAL) using conda command line, restarting the machine and reinstalling the xerces-c package with the dependencies given that you are using Anaconda.

Good luck!

Hasnein Tareque
  • 461
  • 2
  • 11