[GRASS-user] Python scripting

Glynn Clements glynn at gclements.plus.com
Sat Aug 18 10:46:33 PDT 2012


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 at PERMANENT,CA2010 at 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 at gclements.plus.com>


More information about the grass-user mailing list