[GRASS-user] Error trying to run grass from python script/command line in Windows

Hi,

I’m new to grass and I’m having an issue trying to run grass from a python script in my Windows 7 x64 environment.

I’ve read this links

https://grasswiki.osgeo.org/wiki/GRASS_and_Python#MS-Windows

https://grasswiki.osgeo.org/wiki/Working_with_GRASS_without_starting_it_explicitly

https://grass.osgeo.org/grass70/manuals/libpython/script.html#module-script.setup

But I’m not getting this working.

Here is my python script, running a simple g.list command

===
import os
import sys
import subprocess

gisbase = r’C:\OSGeo4W\apps\grass\grass-7.0.4’
os.environ[‘GISBASE’] = gisbase
grass_pydir = os.path.join(gisbase, “etc”, “python”)
sys.path.append(grass_pydir)

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

gisdb = os.path.join(os.path.expanduser(“~”), “Documents/grassdata”)
location = “nc_spm_08”
mapset = “user1”

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

use grass functions here

gscript.run_command(‘g.list -m type=raster’)

os.remove(rcfile)

And this is the stacktrace I’m getting
C:\test>setupgrass.py
Traceback (most recent call last):
File “C:\test\setupgrass.py”, line 20, in
gscript.run_command(‘g.list -m type=raster’)
File “C:\OSGeo4W\apps\grass\grass-7.0.4\etc\python\grass\script\core.py”, line 393, in run_command
ps = start_command(*args, **kwargs)
File “C:\OSGeo4W\apps\grass\grass-7.0.4\etc\python\grass\script\core.py”, line 356, in start_command
if debug_level() > 0:
File “C:\OSGeo4W\apps\grass\grass-7.0.4\etc\python\grass\script\core.py”, line 1519, in debug_level
_debug_level = int(gisenv().get(‘DEBUG’, 0))
File “C:\OSGeo4W\apps\grass\grass-7.0.4\etc\python\grass\script\core.py”, line 935, in gisenv
s = read_command(“g.gisenv”, flags=‘n’)
File “C:\OSGeo4W\apps\grass\grass-7.0.4\etc\python\grass\script\core.py”, line 446, in read_command
return handle_errors(returncode, stdout, args, kwargs)
File “C:\OSGeo4W\apps\grass\grass-7.0.4\etc\python\grass\script\core.py”, line 313, in handle_errors
returncode=returncode)
grass.exceptions.CalledModuleError: Module run None [‘g.gisenv’, ‘-n’] ended with error
Process ended with non-zero return code -1073741502. See errors in the (error) output.

I tried both with Grass standalone installer, and with grass installed through OSGeo4W spackages and I’m getting the same error.

Anyone knows what the problem could be?

Thanks in advance!

Hi,

On Thu, Aug 4, 2016 at 7:50 PM, Jose M <soloninguno@hotmail.com> wrote:

gisbase = r'C:\OSGeo4W\apps\grass\grass-7.0.4'
os.environ['GISBASE'] = gisbase

likely unrelated but I suggest to use the way specified in the manual, i.e.
obtaining the GISBASE value from the binary:

# query GRASS GIS itself for its GISBASEstartcmd = [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)

https://grass.osgeo.org/grass72/manuals/libpython/script.html#module-script.setup

gscript.run_command('g.list -m type=raster')

Unrelated, but this is not guaranteed to work. Use the syntax:

gscript.run_command('g.list', flags='m', type='raster')

And this is the stacktrace I'm getting
C:\test>setupgrass.py
Traceback (most recent call last):
  File "C:\test\setupgrass.py", line 20, in <module>
    gscript.run_command('g.list -m type=raster')
  File "C:\OSGeo4W\apps\grass\grass-7.0.4\etc\python\grass\script\core.py",
line 393, in run_command
    ps = start_command(*args, **kwargs)
  File "C:\OSGeo4W\apps\grass\grass-7.0.4\etc\python\grass\script\core.py",
line 356, in start_command
    if debug_level() > 0:
  File "C:\OSGeo4W\apps\grass\grass-7.0.4\etc\python\grass\script\core.py",
line 1519, in debug_level
    _debug_level = int(gisenv().get('DEBUG', 0))
  File "C:\OSGeo4W\apps\grass\grass-7.0.4\etc\python\grass\script\core.py",
line 935, in gisenv
    s = read_command("g.gisenv", flags='n')
  File "C:\OSGeo4W\apps\grass\grass-7.0.4\etc\python\grass\script\core.py",
line 446, in read_command
    return handle_errors(returncode, stdout, args, kwargs)
  File "C:\OSGeo4W\apps\grass\grass-7.0.4\etc\python\grass\script\core.py",
line 313, in handle_errors
    returncode=returncode)
grass.exceptions.CalledModuleError: Module run None ['g.gisenv', '-n']
ended with error
Process ended with non-zero return code -1073741502. See errors in the
(error) output.

According to my Internet search, -1073741502 means STATUS_DLL_INIT_FAILED -
DLL Initialization Failed. So perhaps the installation is somehow broken or
the paths are not set right. You can try

print os.environ

after init() call to see how the paths are set.

Vaclav

Hi Vaclav,

Thanks for your suggestions

I already tried obtaining the gisbase from the binary but got the same error, so i then switch to the shorthand way (besides, instaling grass through the Osgeo4w doesn’t create a grass70.bat in the

C:\OSGeo4W\apps\grass\grass-7.0.4 folder, the bat seems to be only created using the standalone grass installers).

Using this way I’m getting the same stacktrace.
script.run_command(‘g.list’, flags=‘m’, type=‘raster’)

Running the os.environ shows me several variables, the more relevants I suppose are
‘PYTHONPATH’: ‘C:\OSGeo4W\apps\grass\grass-7.0.4\etc\python’
‘GISBASE’: ‘C:\OSGeo4W\apps\grass\grass-7.0.4’

‘GISRC’: ‘c:\users\Carl\appdata\local\temp\tmprv4iwf’

‘GIS_LOCK’: ‘5156’
‘GRASS_PYTHON’: ‘python.exe’

‘GRASS_ADDON_BASE’: ‘C:\Users\Carl\AppData\Roaming\GRASS7\addons’

‘PATH’: '‘C:\Windows\SYSTEM32;C:\Windows;C:\Windows\SYSTEM32\WBEM;C:\Program Files (x86)\GDAL;C:\Python27;C:\Python27;C:\Python27\Scripts;C:\OSGeo4W\apps\grass\grass-7.0.4\bin;C:\OSGeo4W\apps\grass\grass-7.0.4\scripts;C:\OSGeo4W\apps\grass\grass-7.0.4\extrabin;C:\OSGeo4W\apps\grass\grass-7.0.4\lib’,

‘GDAL_DRIVER_PATH’: ‘C:\Program Files (x86)\GDAL\gdalplugins’
‘GDAL_DATA’: ‘C:\Program Files (x86)\GDAL\gdal-data’
‘SAGA_MLB’: ‘C:\Saga\modules’
‘GISBASE’: ‘C:\OSGeo4W\apps\grass\grass-7.0.4’
‘GDAL_HOME’: ‘C:\Program Files (x86)\GDAL’
‘PYTHON_HOME’: ‘C:\Python27’
‘SAGA_HOME’: ‘C:\Saga’

I forgot to mention in the first mail that before getting the stack trace a popup appear saying this

···

De: Vaclav Petras wenzeslaus@gmail.com
Enviado: jueves, 04 de agosto de 2016 09:37 p.m.
Para: Jose M
Cc: grass-user@lists.osgeo.org
Asunto: Re: [GRASS-user] Error trying to run grass from python script/command line in Windows

Hi,

On Thu, Aug 4, 2016 at 7:50 PM, Jose M <soloninguno@hotmail.com> wrote:

gisbase = r’C:\OSGeo4W\apps\grass\grass-7.0.4’
os.environ[‘GISBASE’] = gisbase

likely unrelated but I suggest to use the way specified in the manual, i.e. obtaining the GISBASE value from the binary:

# query GRASS GIS itself for its GISBASE
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)

https://grass.osgeo.org/grass72/manuals/libpython/script.html#module-script.setup

gscript.run_command(‘g.list -m type=raster’)

Unrelated, but this is not guaranteed to work. Use the syntax:

gscript.run_command(‘g.list’, flags=‘m’, type=‘raster’)

And this is the stacktrace I’m getting
C:\test>setupgrass.py
Traceback (most recent call last):
File “C:\test\setupgrass.py”, line 20, in
gscript.run_command(‘g.list -m type=raster’)
File “C:\OSGeo4W\apps\grass\grass-7.0.4\etc\python\grass\script\core.py”, line 393, in run_command
ps = start_command(*args, **kwargs)
File “C:\OSGeo4W\apps\grass\grass-7.0.4\etc\python\grass\script\core.py”, line 356, in start_command
if debug_level() > 0:
File “C:\OSGeo4W\apps\grass\grass-7.0.4\etc\python\grass\script\core.py”, line 1519, in debug_level
_debug_level = int(gisenv().get(‘DEBUG’, 0))
File “C:\OSGeo4W\apps\grass\grass-7.0.4\etc\python\grass\script\core.py”, line 935, in gisenv
s = read_command(“g.gisenv”, flags=‘n’)
File “C:\OSGeo4W\apps\grass\grass-7.0.4\etc\python\grass\script\core.py”, line 446, in read_command
return handle_errors(returncode, stdout, args, kwargs)
File “C:\OSGeo4W\apps\grass\grass-7.0.4\etc\python\grass\script\core.py”, line 313, in handle_errors
returncode=returncode)
grass.exceptions.CalledModuleError: Module run None [‘g.gisenv’, ‘-n’] ended with error
Process ended with non-zero return code -1073741502. See errors in the (error) output.

According to my Internet search, -1073741502 means STATUS_DLL_INIT_FAILED - DLL Initialization Failed. So perhaps the installation is somehow broken or the paths are not set right. You can try

print os.environ

after init() call to see how the paths are set.

Vaclav

(besides, instaling grass through the Osgeo4w doesn't create a grass70.bat

in the

C:\OSGeo4W\apps\grass\grass-7.0.4 folder, the bat seems to be only created

using the standalone grass >installers).

it does, but it lives in C:\OSGeo4W\bin

-----
best regards
Helmut
--
View this message in context: http://osgeo-org.1560.x6.nabble.com/Error-trying-to-run-grass-from-python-script-command-line-in-Windows-tp5279598p5279620.html
Sent from the Grass - Users mailing list archive at Nabble.com.

I forgot to mention in the first mail that before getting the stack trace a

popup appear saying this

===
Microsoft Visual C++ Runtime Library

Program
C:\OSGeo4W\apps\grass\grass-7.0.4\bin\g.gisenv.EXE

R6034
An application has made an attempt to load the C runtime library

incorrectly.

Please contact the application's support team for more information.

not sure but it may looks like a mismatch of several MS C runtimes used by
different software packages?

-----
best regards
Helmut
--
View this message in context: http://osgeo-org.1560.x6.nabble.com/Error-trying-to-run-grass-from-python-script-command-line-in-Windows-tp5279598p5279621.html
Sent from the Grass - Users mailing list archive at Nabble.com.

What i discover is that if I remove gdal from PATH environment, the error

change to "iconv.dll missing" but >with a similar stacktrace

the OSGeo4W iconv.dll lives in C:\OSGeo4W\bin. is it there in your OSGeo4W
stack.

have a look if there are maybe other iconv.dll in your %PATH%/system (e.g.
system32 in c:\windows or similar); see e.g.
https://trac.osgeo.org/osgeo4w/ticket/420

-----
best regards
Helmut
--
View this message in context: http://osgeo-org.1560.x6.nabble.com/Error-trying-to-run-grass-from-python-script-command-line-in-Windows-tp5279598p5279623.html
Sent from the Grass - Users mailing list archive at Nabble.com.

'PATH':
''C:\\Windows\\SYSTEM32;C:\\Windows;C:\\Windows\\SYSTEM32\\WBEM;C:\\Program
Files
(x86)\\GDAL;C:\\Python27;C:\\Python27;C:\\Python27\\Scripts;C:\\OSGeo4W\\apps\\grass\\grass-7.0.4\\bin;C:\\OSGeo4W\\apps\\grass\\grass-7.0.4\\scripts;C:\\OSGeo4W\\apps\\grass\\grass-7.0.4\\extrabin;C:\\OSGeo4W\\apps\\grass\\grass-7.0.4\\lib',
'GDAL_DRIVER_PATH': 'C:\\Program Files (x86)\\GDAL\\gdalplugins'
'GDAL_DATA': 'C:\\Program Files (x86)\\GDAL\\gdal-data'
'SAGA_MLB': 'C:\\Saga\\modules'
'GISBASE': 'C:\\OSGeo4W\\apps\\grass\\grass-7.0.4'
'GDAL_HOME': 'C:\\Program Files (x86)\\GDAL'
'PYTHON_HOME': 'C:\\Python27'
'SAGA_HOME': 'C:\\Saga'

Looking at your %PATH % variable :

Do you have more GDAL installations on your system in %PATH%?
which may interfer?

-----
best regards
Helmut
--
View this message in context: http://osgeo-org.1560.x6.nabble.com/Error-trying-to-run-grass-from-python-script-command-line-in-Windows-tp5279598p5279644.html
Sent from the Grass - Users mailing list archive at Nabble.com.