[GRASS-user] Python scripting

Hi guys,

  I'm starting to use Python to run an analysis that should take
several hours to process (a few minutes for each individual analysis, that
is why scripting is the way to go) but I'm getting some pretty weird errors
(using the shell inside the GUI).

So I'll start my questions from the beginning:

  - Which is the python library from Grass that I have to reference? I
did not find any library called grass.py (which I'd guess would be called
given how one references it inside the python shell in Grass)
  - Testing the exec_command inside grass' python shell I get the
following:

grass.exec_command('r.stats', flags='', quiet=True, verbose=False, env=None,
input='FAZ_Raster@PERMANENT,CA2010@PERMANENT',
output='C:\Users\Pedro\Downloads\GRASS\California2010\StatsO.txt')

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File
"C:\PROGRA~2\QUANTU~1\apps\grass\grass-6.4.2\etc\python\grass\script\core.py
", line 312, in exec_command
    os.execvpe(prog, args, env)
  File "C:\PROGRA~2\QUANTU~1\apps\Python27\lib\os.py", line 353, in execvpe
    _execvpe(file, args, env)
  File "C:\PROGRA~2\QUANTU~1\apps\Python27\lib\os.py", line 380, in _execvpe
    func(fullname, *argrest)
OSError: [Errno 2] No such file or directory

  Any ideas on where the error might be?

Thanks again,

  Pedro

Pedro Camargo wrote:

  I'm starting to use Python to run an analysis that should take
several hours to process (a few minutes for each individual analysis, that
is why scripting is the way to go) but I'm getting some pretty weird errors
(using the shell inside the GUI).

So I'll start my questions from the beginning:

  - Which is the python library from Grass that I have to reference? I
did not find any library called grass.py (which I'd guess would be called
given how one references it inside the python shell in Grass)

The library for "scripting" is "grass.script", typically used as:

  import grass.script as grass

The files are at $GISBASE/etc/python/grass/script/*.py.

  - Testing the exec_command inside grass' python shell I get the
following:

grass.exec_command('r.stats', flags='', quiet=True, verbose=False, env=None,
input='FAZ_Raster@PERMANENT,CA2010@PERMANENT',
output='C:\Users\Pedro\Downloads\GRASS\California2010\StatsO.txt')

exec_command() is rarely appropriate, as it never returns (the
executed command replaces the script, rather than returning to the
script upon completion). You probably want run_command() instead.

On Windows, exec_command() will probably require the ".exe" suffix.
Unlike the other *_command() functions which use subprocess.Popen(),
exec_command() uses os.execvpe().

--
Glynn Clements <glynn@gclements.plus.com>