1

I'm trying to use GRASS on python 2.7, but I'm having some problems at setting my script on IDLE, then I'm getting an error at parser() function:

Here is my script:

import os
import sys

gisbase = os.environ['GISBASE'] = 'C:\Program Files (x86)\GRASS GIS 7.0.1RC1'
gisrc = 'C:\grassdata'
gisdbase = 'C:\grassdata'
location = 'newLocation'
mapset = 'TC'
LD_LIBRARY_PATH = 'C:\Program Files (x86)\GRASS GIS 7.0.1RC1\lib'
PATH = 'C:\Program Files (x86)\GRASS GIS 7.0.1RC1\etc';'C:\Program Files (x86)\GRASS GIS 7.0.1RC1\etc\python';'C:\Program Files (x86)\GRASS GIS 7.0.1RC1\lib';'C:\Program Files (x86)\GRASS GIS 7.0.1RC1\bin';'C:\Python27';'C:\Program Files (x86)\GRASS GIS 7.0.1RC1\Python27';'C:\Program Files (x86)\GRASS GIS 7.0.1RC1\msys'
PYTHONLIB = 'C:\Python27'
PYTHONPATH = 'C:\Program Files (x86)\GRASS GIS 7.0.1RC1\etc\python'


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

import grass.script as grass

grass.parser() #I'M STUCKING HERE

I'm getting an error inside subprocess.py :

p = subprocess.Popen([prog, '-n'] + argv, stdout=subprocess.PIPE)

The full error:

Traceback (most recent call last):
  File "C:\Users\Ciro\Desktop\teste_grass.py", line 19, in <module>
    grass.parser()
  File "C:\Program Files (x86)\GRASS GIS 7.0.1RC1\etc\python\grass\script\core.py", line 680, in parser
    p = subprocess.Popen([prog, '-n'] + argv, stdout=subprocess.PIPE)
  File "C:\Python27\ArcGIS10.1\lib\subprocess.py", line 679, in __init__
    errread, errwrite)
  File "C:\Python27\ArcGIS10.1\lib\subprocess.py", line 893, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

What am I missing?

João Paulo
  • 6,300
  • 4
  • 51
  • 80
  • Cross-posted to GIS SE http://gis.stackexchange.com/questions/179590/grass-parser-error/179789 (please, always explicitly state that a question was cross-posted). – wenzeslaus Feb 09 '16 at 04:02

1 Answers1

3

Based on the GRASS source, it is trying to run g.parser.exe but is unable to find it. You need to set the PATH environment variable correctly to fix this.

You seem to be trying to set the system environment variables in the bulk of your code as one would do it in bash or batch scripts, if I'm not mistaken. You are currently only setting and modifying Python variables, tough. If you want to actually set the environment variables so that other subprocesses (such as GRASS) can see them, you need to modify the os.environ variable, e.g.

os.environ['PYTHONLIB'] = ...

instead of

PYTHONLIB = ...

Coming back to the problem with the PATH environment variable, you are already updating it with sys.path.append(...) and also setting the right locations in the PATH = ... line above. The latter just does not have any effect due to the reason mentioned above. You should only need to add sys.path.append(PATH) on top of that to get rid of the error you are seeing.

Martin Valgur
  • 5,793
  • 1
  • 33
  • 45