<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<div class="PlainText" style="margin: 0px; font-size: 14.6667px; font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; background-color: rgb(255, 255, 255)">
Hi Markus!</div>
<div class="PlainText" style="margin: 0px; font-size: 14.6667px; font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; background-color: rgb(255, 255, 255)">
<br>
</div>
<div class="PlainText" style="margin: 0px; font-size: 14.6667px; font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; background-color: rgb(255, 255, 255)">
We compiled GRASS 7.8 and this solved the problem of python3/python2 but we got the same error:</div>
<div class="PlainText" style="margin: 0px; font-size: 14.6667px; font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; background-color: rgb(255, 255, 255)">
  </div>
<div class="PlainText" style="margin: 0px; font-size: 14.6667px; font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; background-color: rgb(255, 255, 255)">
File "importgrass78.py", line 79, in <module>
<div style="margin: 0px">    main()</div>
<div style="margin: 0px">  File "importgrass78.py", line 68, in main</div>
<div style="margin: 0px">    rcfile = gsetup.init(gisbase, gisdb, location, mapset)</div>
<div style="margin: 0px">  File "/usr/local/grass78/etc/python/grass/script/setup.py", line 175, in init</div>
<div style="margin: 0px">    config_dir = os.path.join(os.getenv('HOME'), config_dirname)</div>
<div style="margin: 0px">  File "/usr/lib/python3.6/posixpath.py", line 80, in join</div>
<div style="margin: 0px">    a = os.fspath(a)</div>
<br>
</div>
<div class="PlainText" style="margin: 0px; font-size: 14.6667px; font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; background-color: rgb(255, 255, 255)">
This issue is due to running the GRASS script from php since the server is not able to retrieve the HOME path of the user www-data. Therefore, we modified the setup.py script specifying the home path in a new variable:</div>
<div class="PlainText" style="margin: 0px; font-size: 14.6667px; font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; background-color: rgb(255, 255, 255)">
<br>
</div>
<div class="PlainText" style="margin: 0px; font-size: 14.6667px; font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; background-color: rgb(255, 255, 255)">
# add addons to the PATH
<div style="margin: 0px">    # copied and simplified from lib/init/grass.py</div>
<div style="margin: 0px">    if mswin:</div>
<div style="margin: 0px">        config_dirname = "GRASS7"</div>
<div style="margin: 0px">        config_dir = os.path.join(os.getenv('APPDATA'), config_dirname)</div>
<div style="margin: 0px">    else:</div>
<div style="margin: 0px">        <b>user_home = '/home/ubuntu/'</b></div>
<div style="margin: 0px">        config_dirname = ".grass7"</div>
<div style="margin: 0px">        <b>config_dir = os.path.join(user_home, config_dirname)</b></div>
<div class="PlainText" style="margin: 0px"><br>
</div>
<div class="PlainText" style="margin: 0px">After these changes, we were able to run GRASS from a php web page but we got a new error using certain modules like v.in.ogr:</div>
<div class="PlainText" style="margin: 0px"><br>
</div>
<div class="PlainText" style="margin: 0px">Check if OGR layer <sql_statement> contains polygons...
<div style="margin: 0px">   0%^H^H^H^H^H  33%^H^H^H^H^H  66%^H^H^H^H^H 100%^H^H^H^H^H</div>
<div style="margin: 0px">WARNING: Vector map <line2d> already exists and will be overwritten</div>
<div style="margin: 0px">Creating attribute table for layer <sql_statement>...</div>
<div style="margin: 0px">ERROR: Unable to determine user's home directory</div>
<div style="margin: 0px">Traceback (most recent call last):</div>
<div style="margin: 0px">  File "path3d.py", line 297, in <module></div>
<div style="margin: 0px">    main()</div>
<div style="margin: 0px">  File "path3d.py", line 267, in main</div>
<div style="margin: 0px">    gscript.run_command('v.in.ogr', input='{}/tmp/output.gpkg'.format(outdir0), layer='sql_statement$</div>
<div style="margin: 0px">  File "/usr/local/grass78/etc/python/grass/script/core.py", line 441, in run_command</div>
<div style="margin: 0px">    return handle_errors(returncode, returncode, args, kwargs)</div>
<div style="margin: 0px">  File "/usr/local/grass78/etc/python/grass/script/core.py", line 343, in handle_errors</div>
<div style="margin: 0px">    returncode=returncode)</div>
</div>
</div>
<br>
</div>
<div>
<div id="appendonsend"></div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
The error is always the same, the php web page is not able to retrieve the home path. We solved this error setting the server home path as HOME environment variable. Adding the following lines to our GRASS python script, everything works fine and we are now
 able to run the script from our php web page!</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
home = '/home/ubuntu/'    </div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
os.environ['HOME'] = home<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Hope this can help in case anyone has the same problem!<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Regarding the problem installing GRASS GIS from ubuntugis unstable, I don't know exactly what te problem is, but using <span style="font-family: Calibri, Helvetica, sans-serif; background-color: rgb(255, 255, 255); display: inline !important">ubuntugis unstable
 caused</span> some conflicts with Postgis and QGIS server packages respectively installed using <a href="https://wiki.postgresql.org/wiki/Apt" id="LPlnk592384">https://wiki.postgresql.org/wiki/Apt</a> and <a class="reference external" href="https://qgis.org/ubuntu-ltr" style="color: rgb(0, 136, 204); padding-right: 15px; background: url("images/link-icon_external.png") right center no-repeat rgb(255, 255, 255); font-family: "Open Sans", "Helvetica Neue", Ubuntu, Arial, sans-serif; font-size: 14.4px; text-align: left">https://qgis.org/ubuntu-ltr</a></div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Thanks again for yout help and your suggestions!</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Roberta</div>
<hr tabindex="-1" style="display:inline-block; width:98%">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" color="#000000" style="font-size:11pt"><b>Da:</b> Markus Neteler <neteler@osgeo.org><br>
<b>Inviato:</b> domenica 4 ottobre 2020 11:04<br>
<b>A:</b> roberta fagandini <robifagandini@hotmail.it><br>
<b>Cc:</b> Moritz Lennert <mlennert@club.worldonline.be>; grass-user@lists.osgeo.org <grass-user@lists.osgeo.org><br>
<b>Oggetto:</b> Re: [GRASS-user] R: exwcuting grass python script from php web page</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt">
<div class="PlainText">Hi Roberta,<br>
<br>
On Wed, Sep 30, 2020 at 5:12 PM roberta fagandini<br>
<robifagandini@hotmail.it> wrote:<br>
> On 28/09/20 18:37, roberta fagandini wrote:<br>
> > On 28/09/20 17:51, roberta fagandini wrote:<br>
> >> Sorry, maybe I didn't explain myself well because actually, I don't get<br>
> >> any error from the php web page, simply the code related to grass is not<br>
> >> executed.For instance gsetup.init(gisbase, gisdb, location, mapset) does<br>
> >> not create any file in my /tmp/ folder and the print(gscript.gisenv())<br>
> >> is not printed.<br>
> >> I checked the import of the grass libraries (print(sys.modules)) and<br>
> >> they seem to be correctly imported.<br>
> ><br>
> > Have you checked your webservers error logs ?<br>
> ><br>
> > I have already checked and this is the error in the webserver log file<br>
<br>
I observe that:<br>
<br>
> > File "importgrass.py", line 82, in <module><br>
> >      main()<br>
> >    File "importgrass.py", line 71, in main<br>
> >      rcfile = gsetup.init(gisbase, gisdb, location, mapset)<br>
> >    File "/usr/lib/grass74/etc/python/grass/script/setup.py", line 170,<br>
<br>
... you use GRASS GIS 7.4? Pretty old and outdated.<br>
<br>
> > in init<br>
> >      config_dir = os.path.join(os.getenv('HOME'), config_dirname)<br>
> >    File "/usr/lib/python3.6/posixpath.py", line 80, in join<br>
<br>
... you try to use GRASS GIS 7.4 with Python 3. For this you need<br>
GRASS GIS 7.8 as already suggested.<br>
<br>
> >      a = os.fspath(a)<br>
> > TypeError: expected str, bytes or os.PathLike object, not NoneType<br>
...<br>
> I tried to upgrade GRASS using the repository ubuntugis unstable but it caused some problems with<br>
> the installation of postgis.<br>
<br>
Which problems exactly?<br>
<br>
> >         $command = escapeshellcmd('/usr/bin/python3 importgrass.py ');<br>
<br>
... Python 3, fine. But here:<br>
<br>
> I tried to run '/usr/bin/grass --version' but I got a similar error:<br>
><br>
> File "/usr/bin/grass", line 2005, in <module><br>
>     main()<br>
>   File "/usr/bin/grass", line 1804, in main<br>
>     grass_config_dir = get_grass_config_dir()<br>
>   File "/usr/bin/grass", line 398, in get_grass_config_dir<br>
>     directory = os.path.join(os.getenv('HOME'), grass_config_dirname)<br>
>   File "/usr/lib/python2.7/posixpath.py", line 70, in join<br>
<br>
... again Python 2.<br>
<br>
This mixture will not work. Do you have by chance multiple GRASS GIS<br>
versions installed?<br>
<br>
I'd suggest removing all installed GRASS packages and reinstalling 7.8<br>
cleanly again.<br>
<br>
best<br>
Markus<br>
</div>
</span></font></div>
</div>
</body>
</html>