[GRASS-user] GRASS 7.0 Running Scripts (.\GRASS GIS 7.0.0\scripts)

Hello,

How do I run scripts located in C:\Program Files (x86)\GRASS GIS 7.0.0\scripts?

I am learning GRASS 7.0.0 on Windows 7 by converting some GRASS 6.4 shell scripts to GRASS 7 Python scripts (import grass.script as grass) and GRASS 7 PyGRASS scripts (from grass.pygrass.modules import Module).

My installation is working fine. I can manually run the commands (from a shell script) in the “Command console” window of the “Layer Manager”. For example, I can enter “g.region swwake_30m -p” in the “Command prompt” area and get the following results:

Sun Mar 01 12:32:30 2015)
g.region swwake_30m -p
projection: 99 (Lambert Conformal Conic)
zone: 0
datum: nad83
ellipsoid: a=6378137 es=0.006694380022900787
north: 228500
south: 215000
west: 630000
east: 645000
nsres: 30
ewres: 30
rows: 450
cols: 500
cells: 225000
(Sun Mar 01 12:32:31 2015) Command finished (0 sec)

I am using the “Pythonwin” IDE to develop the scripts (C:\Program Files (x86)\GRASS GIS 7.0.0\Python27\lib\site-packages\pythonwin\Pythonwin.exe). In the “GRASS GIS 7.0.0 GUI” Command Prompt window I type “pythonwin” and the IDE starts up.

I have spent several days trying to understand how to create GRASS Python scripts and GRASS PyGRASS scripts from the online documentation(http://grasswiki.osgeo.org/wiki/GRASS_and_Python) and by studying the script samples (C:\Program Files (x86)\GRASS GIS 7.0.0\scripts). I have not been successful in getting example code to run but I am still working through the documentation.

But, I should be able to run scripts located in C:\Program Files (x86)\GRASS GIS 7.0.0\scripts.

Correct?

  1. If I open .\scripts[d.what.rast.py](http://d.what.rast.py) in the Pythonwin IDE and try to run it, I get this output:

OSError error(9): The handle is invalid
Traceback (most recent call last):
File “C:\Program Files (x86)\GRASS GIS 7.0.0\Python27\lib\site-packages\pythonwin\pywin\framework\scriptutils.py”, line 325, in RunScript
exec codeObject in main.dict
File “C:\Users\Rob\Documents\grassscripts\pygrass\make_raster_using_mapcalc.py”, line 8, in
g.message(“Filter elevation map by a threshold…”)
File “C:\Program Files (x86)\GRASS GIS 7.0.0\etc\python\grass\pygrass\modules\shortcuts.py”, line 46, in getattr
return self.cls(‘%s.%s’ % (self.prefix, name.replace(‘_’, ‘.’)))
File “C:\Program Files (x86)\GRASS GIS 7.0.0\etc\python\grass\pygrass\modules\interface\module.py”, line 498, in init
raise GrassError(str_err % self.name)
GrassError: Error running: g.message --interface-description.
Traceback (most recent call last):
File “C:\Program Files (x86)\GRASS GIS 7.0.0\Python27\lib\site-packages\pythonwin\pywin\framework\scriptutils.py”, line 325, in RunScript
exec codeObject in main.dict
File “C:\Program Files (x86)\GRASS GIS 7.0.0\scripts[d.what.rast.py](http://d.what.rast.py)”, line 45, in
main()
File “C:\Program Files (x86)\GRASS GIS 7.0.0\scripts[d.what.rast.py](http://d.what.rast.py)”, line 30, in main
options, flags = gcore.parser()
File “C:\Program Files (x86)\GRASS GIS 7.0.0\etc\python\grass\script\core.py”, line 680, in parser
p = subprocess.Popen([prog, ‘-n’] + argv, stdout=subprocess.PIPE)
File “C:\Program Files (x86)\GRASS GIS 7.0.0\Python27\lib\subprocess.py”, line 703, in init
errread, errwrite) = self._get_handles(stdin, stdout, stderr)
File “C:\Program Files (x86)\GRASS GIS 7.0.0\Python27\lib\subprocess.py”, line 839, in _get_handles
p2cread = self._make_inheritable(p2cread)
File “C:\Program Files (x86)\GRASS GIS 7.0.0\Python27\lib\subprocess.py”, line 878, in _make_inheritable
_subprocess.DUPLICATE_SAME_ACCESS)
WindowsError: [Error 6] The handle is invalid
Traceback (most recent call last):
File “C:\Program Files (x86)\GRASS GIS 7.0.0\Python27\lib\site-packages\pythonwin\pywin\framework\scriptutils.py”, line 325, in RunScript
exec codeObject in main.dict
File “C:\Program Files (x86)\GRASS GIS 7.0.0\scripts[d.what.rast.py](http://d.what.rast.py)”, line 45, in
main()
File “C:\Program Files (x86)\GRASS GIS 7.0.0\scripts[d.what.rast.py](http://d.what.rast.py)”, line 30, in main
options, flags = gcore.parser()
File “C:\Program Files (x86)\GRASS GIS 7.0.0\etc\python\grass\script\core.py”, line 680, in parser
p = subprocess.Popen([prog, ‘-n’] + argv, stdout=subprocess.PIPE)
File “C:\Program Files (x86)\GRASS GIS 7.0.0\Python27\lib\subprocess.py”, line 703, in init
errread, errwrite) = self._get_handles(stdin, stdout, stderr)
File “C:\Program Files (x86)\GRASS GIS 7.0.0\Python27\lib\subprocess.py”, line 839, in _get_handles
p2cread = self._make_inheritable(p2cread)
File “C:\Program Files (x86)\GRASS GIS 7.0.0\Python27\lib\subprocess.py”, line 878, in _make_inheritable
_subprocess.DUPLICATE_SAME_ACCESS)
WindowsError: [Error 6] The handle is invalid

  1. If I go to the “Python shell” tab on the “Layer Manager” and do File → Launch Script and browse to .\scripts[d.what.rast.py](http://d.what.rast.py), I get this output:

(Sun Mar 01 12:32:31 2015) Command finished (0 sec)
Launching script ‘C:\Program Files (x86)\GRASS GIS 7.0.0\scripts[d.what.rast.py](http://d.what.rast.py)’…
Traceback (most recent call last):
File “C:\Program Files (x86)\GRASS GIS
7.0.0\gui\wxpython\gui_core\forms.py”, line 718, in OnRun

gcmd.Command(cmd)
File “C:\Program Files (x86)\GRASS GIS
7.0.0\gui\wxpython\core\gcmd.py”, line 397, in init

_("Error: ") + self.__GetError()))
core.gcmd
.
GException
:
Execution failed: ‘d.what.rast map=elevation’
Details:
Error:

Any ideas on what might be going on here?

Rob Parsons
Raleigh NC

On Sun, Mar 1, 2015 at 7:37 PM, Rob Parsons <jrobparsons@gmail.com> wrote:

Hello,

How do I run scripts located in C:\Program Files (x86)\GRASS GIS
7.0.0\scripts?

I am learning GRASS 7.0.0 on Windows 7 by converting some GRASS 6.4 shell
scripts to GRASS 7 Python scripts (import grass.script as grass) and GRASS
7 PyGRASS scripts (from grass.pygrass.modules import Module).

My installation is working fine. I can manually run the commands (from a
shell script) in the "Command console" window of the "Layer Manager". For
example, I can enter "g.region swwake_30m -p" in the "Command prompt" area
and get the following results:

Sun Mar 01 12:32:30
2015)
g.region swwake_30m
-p
projection: 99 (Lambert Conformal Conic)
zone: 0
datum: nad83
ellipsoid: a=6378137 es=0.006694380022900787
north: 228500
south: 215000
west: 630000
east: 645000
nsres: 30
ewres: 30
rows: 450
cols: 500
cells: 225000
(Sun Mar 01 12:32:31 2015) Command finished (0
sec)

I am using the "Pythonwin" IDE to develop the scripts (C:\Program Files
(x86)\GRASS GIS 7.0.0\Python27\lib\site-packages\pythonwin\Pythonwin.exe).
In the "GRASS GIS 7.0.0 GUI" Command Prompt window I type "pythonwin" and
the IDE starts up.

I have spent several days trying to understand how to create GRASS Python
scripts and GRASS PyGRASS scripts from the online documentation(
http://grasswiki.osgeo.org/wiki/GRASS_and_Python) and by studying the
script samples (C:\Program Files (x86)\GRASS GIS 7.0.0\scripts). I have not
been successful in getting example code to run but I am still working
through the documentation.

Please try the example from:

http://grass.osgeo.org/grass70/manuals/libpython/script_intro.html

I linked this page from documentation as well as the other Python wiki page
from the GRASS and Python page:

http://grasswiki.osgeo.org/wiki/GRASS_Python_Scripting_Library

Note that GRASS scripts by default cannot run from outside GRASS GIS
session (environment). This is probably not emphasized enough in the
documentation.

But, I should be able to run scripts located in C:\Program Files

(x86)\GRASS GIS 7.0.0\scripts.

Correct?

Correct but can you please try some other module than a d.* module, e.g.

r.out.xyz? d.* commands are somehow special (although in this case it
probably doesn't matter).

1. If I open .\scripts\d.what.rast.py in the Pythonwin IDE and try to run
it, I get this output:

OSError error(9): The handle is invalid
Traceback (most recent call last):
  File "C:\Program Files (x86)\GRASS GIS
7.0.0\Python27\lib\site-packages\pythonwin\pywin\framework\scriptutils.py",
line 325, in RunScript
    exec codeObject in __main__.__dict__
  File
"C:\Users\Rob\Documents\grassscripts\pygrass\make_raster_using_mapcalc.py",
line 8, in <module>
    g.message("Filter elevation map by a threshold...")
  File "C:\Program Files (x86)\GRASS GIS
7.0.0\etc\python\grass\pygrass\modules\shortcuts.py", line 46, in
__getattr__
    return self.cls('%s.%s' % (self.prefix, name.replace('_', '.')))
  File "C:\Program Files (x86)\GRASS GIS
7.0.0\etc\python\grass\pygrass\modules\interface\module.py", line 498, in
__init__
    raise GrassError(str_err % self.name)
GrassError: Error running: `g.message --interface-description`.
Traceback (most recent call last):
  File "C:\Program Files (x86)\GRASS GIS
7.0.0\Python27\lib\site-packages\pythonwin\pywin\framework\scriptutils.py",
line 325, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Program Files (x86)\GRASS GIS 7.0.0\scripts\d.what.rast.py",
line 45, in <module>
    main()
  File "C:\Program Files (x86)\GRASS GIS 7.0.0\scripts\d.what.rast.py",
line 30, in main
    options, flags = gcore.parser()
  File "C:\Program Files (x86)\GRASS GIS
7.0.0\etc\python\grass\script\core.py", line 680, in parser
    p = subprocess.Popen([prog, '-n'] + argv, stdout=subprocess.PIPE)
  File "C:\Program Files (x86)\GRASS GIS
7.0.0\Python27\lib\subprocess.py", line 703, in __init__
    errread, errwrite) = self._get_handles(stdin, stdout, stderr)
  File "C:\Program Files (x86)\GRASS GIS
7.0.0\Python27\lib\subprocess.py", line 839, in _get_handles
    p2cread = self._make_inheritable(p2cread)
  File "C:\Program Files (x86)\GRASS GIS
7.0.0\Python27\lib\subprocess.py", line 878, in _make_inheritable
    _subprocess.DUPLICATE_SAME_ACCESS)
WindowsError: [Error 6] The handle is invalid
Traceback (most recent call last):
  File "C:\Program Files (x86)\GRASS GIS
7.0.0\Python27\lib\site-packages\pythonwin\pywin\framework\scriptutils.py",
line 325, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Program Files (x86)\GRASS GIS 7.0.0\scripts\d.what.rast.py",
line 45, in <module>
    main()
  File "C:\Program Files (x86)\GRASS GIS 7.0.0\scripts\d.what.rast.py",
line 30, in main
    options, flags = gcore.parser()
  File "C:\Program Files (x86)\GRASS GIS
7.0.0\etc\python\grass\script\core.py", line 680, in parser
    p = subprocess.Popen([prog, '-n'] + argv, stdout=subprocess.PIPE)
  File "C:\Program Files (x86)\GRASS GIS
7.0.0\Python27\lib\subprocess.py", line 703, in __init__
    errread, errwrite) = self._get_handles(stdin, stdout, stderr)
  File "C:\Program Files (x86)\GRASS GIS
7.0.0\Python27\lib\subprocess.py", line 839, in _get_handles
    p2cread = self._make_inheritable(p2cread)
  File "C:\Program Files (x86)\GRASS GIS
7.0.0\Python27\lib\subprocess.py", line 878, in _make_inheritable
    _subprocess.DUPLICATE_SAME_ACCESS)
WindowsError: [Error 6] The handle is invalid
>>>

This might be somehow related to bug in Python itself:

http://bugs.python.org/issue3905

However, please focus on running the scripts for GRASS GIS from GRASS
session. The easiest way how to do that on MS Windows is to write the
script and then to start it from GUI (File > Run script) or from Python
shell tab in the GUI.

It is possible to run scripts which are using GRASS modules from outside of
GRASS session but it requires you to set up the GRASS environment yourself.
This is possible but it has some challenges. On the other hand, if you
write your script in the way that it runs (only) in a GRASS session you can
use GRASS features for automatic building of interface and thus create a
true GRASS module.

2. If I go to the "Python shell" tab on the "Layer Manager" and do File ->
Launch Script and browse to .\scripts\d.what.rast.py, I get this output:

(Sun Mar 01 12:32:31 2015) Command finished (0
sec)
Launching script 'C:\Program Files (x86)\GRASS GIS 7.0.0\scripts\
d.what.rast.py'...
Traceback (most recent call last):
  File "C:\Program Files (x86)\GRASS GIS
7.0.0\gui\wxpython\gui_core\forms.py", line 718, in OnRun

gcmd.Command(cmd)
  File "C:\Program Files (x86)\GRASS GIS
7.0.0\gui\wxpython\core\gcmd.py", line 397, in __init__

_("Error: ") + self.__GetError()))
core.gcmd
.
GException
:
Execution failed: 'd.what.rast map=elevation'
Details:
Error:

The error message seems to be incomplete but in any case, please try with

r.out.xyz script and the example script from:

http://grass.osgeo.org/grass70/manuals/libpython/script_intro.html

Vaclav

Any ideas on what might be going on here?

Rob Parsons
Raleigh NC

_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user