[GRASS-user] Problem with python script in osgeo4w grass 6.4.4 32bit

Dear all,

i made a python script for grass and it does not work in grass (version in message subject).
The interface opens but a Windows error states that it is impossible to open the file, the
file is hence not recognized as a valid command.
the script used to work with osgeo4w 6.4.3 32 bit, if copied in “…/etc/gui/scripts” directory.

I understand (various messages found with issues about python scripts in windows)
this may be a big problem, but i wish i can help to debug this, or maybe
is it better to jump to grass version 7 ?

thanks,
Roy.

m roy wrote

Dear all,

i made a python script for grass and it does not work in grass (version in
message subject).
The interface opens but a Windows error states that it is impossible to
open the file, the
file is hence not recognized as a valid command.
the script used to work with osgeo4w 6.4.3 32 bit, if copied in
"../etc/gui/scripts" directory.

so far I know nothing about your script.

why is your script in "../etc/gui/scripts"? as it states this folder is for
GUI scripts; AFAIR this folder may be not in %PATH%.

have you any bat-wrapper for your python script in your %PATH%.

scripts's are usually in C:\OSGeo4W\apps\grass\grass-6.4.x\scripts

e.g.

d.correlate
d.font.freetype
[...]
v.what.vect

and related bat-wrapper (with appropriatly set path to the scripts) are in

C:\OSGeo4W\apps\grass\grass-6.4.x\bin

e.g.

d.correlate.bat

{
@"%GRASS_SH%" "%GISBASE%/scripts/d.correlate" %*
}

d.font.freetype.bat

{
@"%GRASS_SH%" "%GISBASE%/scripts/d.font.freetype" %*
}

[...]
v.what.vect.bat

{
@"%GRASS_SH%" "%GISBASE%/scripts/v.what.vect" %*
}

for your python script r.your.script.py living in
C:\OSGeo4W\apps\grass\grass-6.4.x\scripts the bat-wrapper-script
r.your.script.bat should be in C:\OSGeo4W\apps\grass\grass-6.4.x\bin
with following content

{
@"%GRASS_PYTHON%" "%GISBASE%/scripts/r.your.script.py" %*
}

-----
best regards
Helmut
--
View this message in context: http://osgeo-org.1560.x6.nabble.com/Problem-with-python-script-in-osgeo4w-grass-6-4-4-32bit-tp5170744p5170781.html
Sent from the Grass - Users mailing list archive at Nabble.com.

Dear Helmut,
thanks for your answer

Date: Sat, 1 Nov 2014 15:17:32 -0700
From: hellik@web.de
To: grass-user@lists.osgeo.org
Subject: Re: [GRASS-user] Problem with python script in osgeo4w grass 6.4.4 32bit

m roy wrote

Dear all,

i made a python script for grass and it does not work in grass (version in
message subject).
The interface opens but a Windows error states that it is impossible to
open the file, the
file is hence not recognized as a valid command.
the script used to work with osgeo4w 6.4.3 32 bit, if copied in
“…/etc/gui/scripts” directory.

so far I know nothing about your script.

the script is a grass python script i made to automate some processing
and fondamentally uses r.mapcalc to perform map algebra e.g:

grass.mapcalc(“$mapoutfile = abs($inraster)”,mapoutfile=outraster+‘oname’+kf.name,inraster=outraster+‘_’+ofile.name)

why is your script in “…/etc/gui/scripts”? as it states this folder is for
GUI scripts; AFAIR this folder may be not in %PATH%.
This beacause the script have an interface generated by grass parser,
and that is the only way I found to have it run in MS Windows (that worked in grass 6.4.3)
also the scrip flawlessly works in linux.

have you any bat-wrapper for your python script in your %PATH%.
for your python script r.your.script.py living in
C:\OSGeo4W\apps\grass\grass-6.4.x\scripts the bat-wrapper-script
r.your.script.bat should be in C:\OSGeo4W\apps\grass\grass-6.4.x\bin
with following content

{
@“%GRASS_PYTHON%” “%GISBASE%/scripts/r.your.script.py” %*
}

I tried your advice, copied the script in

C:\OSGeo4W\apps\grass\grass-6.4.4\scripts

and made the bat file in

C:\OSGeo4W\apps\grass\grass-6.4.x\bin

but tring tu run it i get this error:

grass output:

Traceback (most recent call last):
File “C:\OSGeo4W\apps\grass\grass-6.4.4\etc\wxpython\gui_c
ore\prompt.py”, line 840, in OnItemSelected

self.cmdDesc = gtask.parse_interface(GetRealCmd(cmd))
File “C:\OSGeo4W\apps\grass\grass-6.4.4\etc\python\grass\s
cript\task.py”, line 474, in parse_interface

tree = etree.fromstring(get_interface_description(name))
File “C:\OSGeo4W\apps\grass\grass-6.4.4\etc\python\grass\s
cript\task.py”, line 452, in get_interface_description

“\n\nDetails: %(det)s”) % { ‘cmd’ : cmd, ‘det’ :
decode(cmderr) }
grass.script.core
.
ScriptError

end

thanks, Roy.

why is your script in "../etc/gui/scripts"? as it states this folder is

for

GUI scripts; AFAIR this folder may be not in %PATH%.

This beacause the script have an interface generated by grass parser,

all scripts in C:\OSGeo4W\apps\grass\grass-6.4.x\scripts have an
autogenerated GUI/interface by grass parser.

I tried your advice, copied the script in

C:\OSGeo4W\apps\grass\grass-6.4.4\scripts

and made the bat file in

C:\OSGeo4W\apps\grass\grass-6.4.x\bin

but tring tu run it i get this error:

tested here on my side:

(1) copied the content of e.g.
http://trac.osgeo.org/grass/browser/grass-addons/grass6/raster/r.basin/r.basin.py

in r.my.testscript.py and saved the python script in
C:\OSGeo4W\apps\grass\grass-6.4.x\scripts

(2) in C:\OSGeo4W\apps\grass\grass-6.4.x\bin I've added r.my.testscript.bat
with following content

@"%GRASS_PYTHON%" "%GISBASE%/scripts/r.my.testscript.py" %*

=> it works without any problem

##
grass.mapcalc("$mapoutfile =

abs($inraster)",mapoutfile=outraster+'_oname_'+kf.__name__,inraster=outraster+'_'+ofile.__name__)

##

not tested, but I'm sure that this work.

please have a look in existing python scripts for grass 6 e.g.
http://trac.osgeo.org/grass/browser/grass-addons/grass6/raster/r.basin/r.basin.py
and search how variables in grass.mapcalc are handled there

e.g.
86 def main():
87 r_elevation = options['map'].split('@')[0]
[...]
95 prefix = options['prefix']+'_'+mapname[0]
96 r_accumulation = prefix+'_accumulation'
[...]
114 r_mask = prefix+'_mask'
[...]
182 grass.mapcalc("tmp = $r_accumulation / $r_mask",
183 r_accumulation = r_accumulation,
184 r_mask = r_mask)

-----
best regards
Helmut
--
View this message in context: http://osgeo-org.1560.x6.nabble.com/Problem-with-python-script-in-osgeo4w-grass-6-4-4-32bit-tp5170744p5170806.html
Sent from the Grass - Users mailing list archive at Nabble.com.

Helmut Kudrnovsky wrote

##
grass.mapcalc("$mapoutfile =

abs($inraster)",mapoutfile=outraster+'_oname_'+kf.__name__,inraster=outraster+'_'+ofile.__name__)

##

not tested, but I'm sure that this work.

corrected, it should be: not tested, but I'm not sure that this work

-----
best regards
Helmut
--
View this message in context: http://osgeo-org.1560.x6.nabble.com/Problem-with-python-script-in-osgeo4w-grass-6-4-4-32bit-tp5170744p5170810.html
Sent from the Grass - Users mailing list archive at Nabble.com.

Dear Helmut,

now the script works thank you for your help!!

at the first try it didn’t work because i added the .bat line with “braces {}”

misunderstanding your previous advice.

Thanks again, Roy.

···

Il 02/11/2014 11:59, Helmut Kudrnovsky ha scritto:

I tried your advice, copied the script in

C:\OSGeo4W\apps\grass\grass-6.4.4\scripts

and made the bat file in

C:\OSGeo4W\apps\grass\grass-6.4.x\bin

but tring tu run it i get this error:

tested here on my side:

(1) copied the content of e.g. 
[http://trac.osgeo.org/grass/browser/grass-addons/grass6/raster/r.basin/r.basin.py](http://trac.osgeo.org/grass/browser/grass-addons/grass6/raster/r.basin/r.basin.py)

in r.my.testscript.py and saved the python script in
C:\OSGeo4W\apps\grass\grass-6.4.x\scripts

(2) in C:\OSGeo4W\apps\grass\grass-6.4.x\bin I've added r.my.testscript.bat
with following content

@"%GRASS_PYTHON%" "%GISBASE%/scripts/r.my.testscript.py" %*

=> it works without any problem