<div dir="ltr">Hello Carmine, what I do is create a script (connect.py) to start session as follows:<div><br></div><div><div># -*- coding: utf-8 -*-</div><div> </div><div>import os</div><div>import sys</div><div><br></div><div>def launch(location,mapset,db):</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span># path to the GRASS GIS launch script</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>grass7bin = '/usr/bin/grass72'</div><div><br></div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span># Set GISDBASE environment variable</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>os.environ['GISDBASE'] = gisdb</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span></div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span># query GRASS 7 itself for its GISBASE</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>gisbase = "/usr/lib/grass72"</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>os.environ['GISBASE'] = gisbase</div><div><br></div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span># define GRASS-Python environment</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>gpydir = "/usr/lib/grass72/etc/python"</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>sys.path.append(gpydir)</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span></div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span># import GRASS Python bindings</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>import grass.script.setup as gsetup</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span></div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>############ launch session</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>gsetup.init(gisbase, gisdb, location, mapset)</div><div><br></div></div><div><br></div><div><br></div><div><span style="white-space:pre">After that I call the function from my main script and execute the functions as follows</span></div><div><span style="white-space:pre"><br></span></div><div><span style="white-space:pre"><br></span></div><div><span style="white-space:pre">#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import connect #this is the script to connect with grass </span></div><div><span style="white-space:pre"><br></span></div><div><span style="white-space:pre">gisdb = "/path/to/the/main/folder/"</span></div><div><span style="white-space:pre">location = "</span><span style="white-space:pre">/path/to/the/</span><span style="white-space:pre">location/"</span></div><div><span style="white-space:pre">mapset = "</span><span style="white-space:pre">/path/to/the/</span><span style="white-space:pre">mapset/"</span></div><div><span style="white-space:pre"><br></span></div><div><span style="white-space:pre">#Connect
connect.launch(location,mapset,gisdb)
import grass.script as gscript</span><br></div><div><span style="white-space:pre"><br></span></div><div><span style="white-space:pre">#Now I can execute the functions using gscript.run_command as follows:</span></div><div><span style="white-space:pre"><br></span></div><div><span style="white-space:pre">#Load map</span></div><div><span style="white-space:pre">gscript.run_command("r.in.gdal", input="input image", flags='e' ,output = "map name")</span><br></div><div><span style="white-space:pre"><br></span></div><div><span style="white-space:pre">#Set region
gscript.run_command('g.region', n=north, s=south, e=east, w=west, ewres=resX, nsres=resY)</span></div><div><span style="white-space:pre"><br></span></div><div><span style="white-space:pre"><br></span></div><div><span style="white-space:pre"><br></span></div><div><span style="white-space:pre">I hope this helps</span></div><div><span style="white-space:pre"><br></span></div><div><span style="white-space:pre">Regards,</span></div><div><span style="white-space:pre"><br></span></div><div><span style="white-space:pre">Andrés.</span></div><div><span style="white-space:pre"><br></span></div></div><div class="gmail_extra"><br><div class="gmail_quote">2017-03-16 12:53 GMT-03:00 Carmine Massarelli <span dir="ltr"><<a href="mailto:carmine.massarelli@ba.irsa.cnr.it" target="_blank">carmine.massarelli@ba.irsa.cnr.it</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000">
<font face="Ubuntu">Dear,<br>
I have prepared a couple of scripts to execute a batch job: the
first opens a csv file and formats the data for the second
(invoked from the first) that include some grass functionality.<br>
The second script uses grass functionality without starting grass
explicitly.</font><br>
<font face="Ubuntu">If I execute the first script from shell all it
is working, but if i invoke it from a php button
(shell_exec('python path/script.py')) I receive the error: </font><font face="Ubuntu"><i>Cannot find GRASS GIS 7 start script (['grass72',
'--config', 'path'])</i><br>
I saw that it is related to subprocess (here the code):<br>
<br>
<i>p = subprocess.Popen(startcmd,
shell=False,stdout=subprocess.<wbr>PIPE, stderr=subprocess.PIPE)</i><i><br>
</i><i>out, err = p.communicate()</i><i><br>
</i><i>if p.returncode != 0:</i><i><br>
</i><i> print >>sys.stderr, "ERROR: Cannot find GRASS GIS
7 start script (%s)" % startcmd</i><i><br>
</i><i> sys.exit(-1)</i><i><br>
</i><i>gisbase = out.strip('\n\r')</i><br>
<br>
but I am not expert about this, so I ask for your help.<br>
What could I do?<br>
I have already set sudoers www-data ALL=NOPASSWD: ALL and
checked permission.<br>
Thanks in advance<span class="HOEnZb"><font color="#888888"><br>
<br>
carmine<br>
</font></span></font>
<div class="m_8746496830545291321moz-signature"><br>
</div>
</div>
<br>______________________________<wbr>_________________<br>
grass-user mailing list<br>
<a href="mailto:grass-user@lists.osgeo.org">grass-user@lists.osgeo.org</a><br>
<a href="https://lists.osgeo.org/mailman/listinfo/grass-user" rel="noreferrer" target="_blank">https://lists.osgeo.org/<wbr>mailman/listinfo/grass-user</a><br></blockquote></div><br></div>