[GRASS-user] Write python grass program with osgeo4w

Hello list,

A coworker and I have been trying to use the first example in http://download.osgeo.org/grass/grass6_progman/pythonlib.html. We discovered that “import grass.script as grass” has to be changed to “import grass”. Below is the example and the error we are getting is this:

C:>m.test.py
Traceback (most recent call last):
File “C:\OSGeo4W\apps\grass\GRASS-~1.0SV/scripts\m.test.py”, line 31, in
options, flags = grass.parser()
File “C:\OSGeo4W\apps\grass\GRASS-~1.0SV\etc\python\grass.py”, line 208, in parser
os.execvp(“g.parser”, [name] + argv)
File “C:\OSGeo4W\apps\Python25\lib\os.py”, line 353, in execvp
_execvpe(file, args)
File “C:\OSGeo4W\apps\Python25\lib\os.py”, line 389, in _execvpe
func(fullname, *argrest)
OSError: [Errno 22] Invalid argument

Any ideas what is going on?

Bob

#====================================================
#!/usr/bin/env python

#%Module
#% description: Checks if vector map is 3D
#% keywords: vector
#%End
#%option
#% key: map
#% type: string
#% gisprompt: old,vector,vector
#% key_desc: name
#% description: Name of vector map
#% required: yes
#%end

import sys
import grass

def main():
info = grass.parse_command(‘v.info’,
flags = ‘t’,
map = options[‘map’])
if info[‘map3d’] == ‘1’:
print ‘Vector map is 3D’
else:
print ‘Vector map is 2D’

return 0

if name == “main”:
options, flags = grass.parser()
sys.exit(main())

Bob Moskovitz
Research Analyst I
Seismic Hazard Zonation Project
California Geological Survey
http://www.conservation.ca.gov/cgs/shzp

CONFIDENTIALITY NOTICE: This communication is intended only for the use of the individual or entity to which it is addressed. This message contains information from the State of California, California Geological Survey, which may be privileged, confidential and exempt from disclosure under applicable law, including the Electronic Communications Privacy Act. If the reader of this communication is not the intended recipient, you are hereby notified that any dissemination, distribution, or copying of this communication is strictly prohibited.

Moskovitz, Bob wrote:

A coworker and I have been trying to use the first example in
<http://download.osgeo.org/grass/grass6_progman/pythonlib.html&gt;
http://download.osgeo.org/grass/grass6_progman/pythonlib.html. We
discovered that "import grass.script as grass" has to be changed to
"import grass". Below is the example and the error we are getting is
this:

C:\>m.test.py
Traceback (most recent call last):
  File "C:\OSGeo4W\apps\grass\GRASS-~1.0SV/scripts\m.test.py", line 31, in <module>
    options, flags = grass.parser()
  File "C:\OSGeo4W\apps\grass\GRASS-~1.0SV\etc\python\grass.py", line 208, in parser
    os.execvp("g.parser", [name] + argv)
  File "C:\OSGeo4W\apps\Python25\lib\os.py", line 353, in execvp
    _execvpe(file, args)
  File "C:\OSGeo4W\apps\Python25\lib\os.py", line 389, in _execvpe
    func(fullname, *argrest)
OSError: [Errno 22] Invalid argument

Any ideas what is going on?

This is a bug in grass.py; Windows requires "g.parser.exe" rather than
"g.parser". This was fixed with:

  http://trac.osgeo.org/grass/changeset/37075

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

I see that GRASS in OSGEO4W is only up to r36506. Hopefully there will be an update to OSGEO4W soon...maybe even an update to GRASS 6.4 RC5 :slight_smile:

-----Original Message-----
From: Glynn Clements [mailto:glynn@gclements.plus.com]
Sent: Sat 6/13/2009 7:56 AM
To: Moskovitz, Bob
Cc: Grass-User (E-mail)
Subject: Re: [GRASS-user] Write python grass program with osgeo4w

Moskovitz, Bob wrote:

A coworker and I have been trying to use the first example in
<http://download.osgeo.org/grass/grass6_progman/pythonlib.html&gt;
http://download.osgeo.org/grass/grass6_progman/pythonlib.html. We
discovered that "import grass.script as grass" has to be changed to
"import grass". Below is the example and the error we are getting is
this:

C:\>m.test.py
Traceback (most recent call last):
  File "C:\OSGeo4W\apps\grass\GRASS-~1.0SV/scripts\m.test.py", line 31, in <module>
    options, flags = grass.parser()
  File "C:\OSGeo4W\apps\grass\GRASS-~1.0SV\etc\python\grass.py", line 208, in parser
    os.execvp("g.parser", [name] + argv)
  File "C:\OSGeo4W\apps\Python25\lib\os.py", line 353, in execvp
    _execvpe(file, args)
  File "C:\OSGeo4W\apps\Python25\lib\os.py", line 389, in _execvpe
    func(fullname, *argrest)
OSError: [Errno 22] Invalid argument

Any ideas what is going on?

This is a bug in grass.py; Windows requires "g.parser.exe" rather than
"g.parser". This was fixed with:

  http://trac.osgeo.org/grass/changeset/37075

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

I changed grass.py with the changes in changeset 37075. Although I'm no longer getting any error messages, I am not able to see my script help by simply typing "m.test.py --help".

When I try "python C:\OSGeo4W\apps\grass\grass-6.4.0svn\scripts\m.test.py --help", I do see the help. Without "--help", m.test.py doesn't start up in a gui.

What must be done to make python scripts to behave like regular grass commands?

Bob

-----Original Message-----
From: Glynn Clements [mailto:glynn@gclements.plus.com]
Sent: Saturday, June 13, 2009 7:56 AM
To: Moskovitz, Bob
Cc: Grass-User (E-mail)
Subject: Re: [GRASS-user] Write python grass program with osgeo4w

Moskovitz, Bob wrote:

> A coworker and I have been trying to use the first example in
> <http://download.osgeo.org/grass/grass6_progman/pythonlib.html&gt;
> http://download.osgeo.org/grass/grass6_progman/pythonlib.html. We
> discovered that "import grass.script as grass" has to be changed to
> "import grass". Below is the example and the error we are
getting is
> this:
>
> C:\>m.test.py
> Traceback (most recent call last):
> File
"C:\OSGeo4W\apps\grass\GRASS-~1.0SV/scripts\m.test.py", line
31, in <module>
> options, flags = grass.parser()
> File
"C:\OSGeo4W\apps\grass\GRASS-~1.0SV\etc\python\grass.py",
line 208, in parser
> os.execvp("g.parser", [name] + argv)
> File "C:\OSGeo4W\apps\Python25\lib\os.py", line 353, in execvp
> _execvpe(file, args)
> File "C:\OSGeo4W\apps\Python25\lib\os.py", line 389, in _execvpe
> func(fullname, *argrest)
> OSError: [Errno 22] Invalid argument
>
>
> Any ideas what is going on?

This is a bug in grass.py; Windows requires "g.parser.exe" rather than
"g.parser". This was fixed with:

  http://trac.osgeo.org/grass/changeset/37075

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