[GRASS-user] Using grass.script in Jupyter (Azure)

Hi Everyone,

I want to develop a program using GRASS python libraries in Jupyter(on Azure). All codes begin with:

import grass.script as gscript

of course the grass.script is not available in Jupyter. I tried to install GRASS

I used: !pip install GRASS

and it worked like a charm, although I can’t have access to grass.script.

Sorry if it is a silly question, although I apreciate any idea to begin programming GRASS on Azure.

I appreciate any help you can provide,

Mehrdad

-- 

···

Mehrdad Varedi

Hi,

Mehrdad Varedi <varedi@gmail.com> schrieb am Mi., 10. Okt. 2018, 03:25:

Hi Everyone,

I want to develop a program using GRASS python libraries in Jupyter(on Azure). All codes begin with:

import grass.script as gscript

of course the grass.script is not available in Jupyter. I tried to install GRASS

I used: !pip install GRASS

and it worked like a charm, although I can’t have access to grass.script.

Sorry if it is a silly question, although I apreciate any idea to begin programming GRASS on Azure.

Please check the Jupyter examples here:

https://grasswiki.osgeo.org/wiki/GRASS_GIS_Jupyter_notebooks

They may give you a start. Please let us know how it goes and feel free to add your notes to the wiki page.

Best,
Markus

On Tue, Oct 9, 2018 at 9:25 PM Mehrdad Varedi <varedi@gmail.com> wrote:

Hi Everyone,

I want to develop a program using GRASS python libraries in Jupyter(on Azure). All codes begin with:

import grass.script as gscript

of course the grass.script is not available in Jupyter. I tried to install GRASS

I used: !pip install GRASS

and it worked like a charm, although I can’t have access to grass.script.

Sorry if it is a silly question, although I apreciate any idea to begin programming GRASS on Azure.

Hi Mehrdad,

the package you installed is not GRASS GIS. Install GRASS GIS using whatever is appropriate for your platform (e.g. using apt install grass on Ubuntu, but ideally after connecting PPA in this case). Then there is couple of approaches how to use GRASS GIS with Jupyter. The one I prefer is starting Jupyter Notebook and than starting GRASS GIS from there:

https://github.com/wenzeslaus/geospatial-modeling-course-jupyter/blob/master/notebooks/buffers_cost_python.ipynb

See also:

[GRASS-dev] Package grass installed by pip is not GRASS GIS

https://lists.osgeo.org/pipermail/grass-dev/2018-October/090191.html

Best,

Vaclav

I appreciate any help you can provide,

Mehrdad

-- 

Mehrdad Varedi


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

Hi Vaclav,
Thanks for your email, I could run the following lines with no problem in Jupyter:

import os
import sys
import subprocess
from IPython.display import Image

create GRASS GIS runtime environment

gisbase = “f:\PROGRA~1\QGIS2~1.18\apps\grass\grass-7.4.1”.strip()
os.environ[‘GISBASE’] = gisbase
sys.path.append(os.path.join(gisbase, “etc”, “python”))

although when tried to run the following:

import grass.script as gs
import grass.script.setup as gsetup

again I got some errors:

ModuleNotFoundError                       Traceback (most recent call last)
f:\PROGRA~1\QGIS2~1.18\apps\grass\grass-7.4.1\etc\python\grass\script\core.py in <module>()
     38     # python2
---> 39     import __builtin__
     40     from os import environ

ModuleNotFoundError: No module named '__builtin__'

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
<ipython-input-3-cd30798f91ed> in <module>()
----> 1 import grass.script as gs
      2 import grass.script.setup as gsetup

f:\PROGRA~1\QGIS2~1.18\apps\grass\grass-7.4.1\etc\python\grass\script\__init__.py in <module>()
      3 from __future__ import absolute_import
      4 
----> 5 from .core   import *
      6 from .db     import *
      7 from .raster import *

f:\PROGRA~1\QGIS2~1.18\apps\grass\grass-7.4.1\etc\python\grass\script\core.py in <module>()
     42     # python3
     43     import builtins as __builtin__
---> 44     from os import environb as environ
     45     unicode = str
     46 __builtin__.__dict__['_'] = __builtin__.__dict__['_'].__self__.lgettext

ImportError: cannot import name 'environb' from 'os' (f:\ProgramData\Anaconda3\lib\os.py)



Do you have any idea why it is happening?


I have windows 10 on this machine and grass version 7.4.1 is installed.

Thanks,

Mehrdad

···

Mehrdad Varedi

On Sat, Oct 13, 2018 at 9:09 PM Mehrdad Varedi <varedi@gmail.com> wrote:

ImportError Traceback (most recent call last)
in ()
----> 1 import grass.script as gs
2 import grass.script.setup as gsetup

f:\PROGRA~1\QGIS2~1.18\apps\grass\grass-7.4.1\etc\python\grass\script_init_.py in ()
3 from future import absolute_import
4
----> 5 from .core import *
6 from .db import *
7 from .raster import *

f:\PROGRA~1\QGIS2~1.18\apps\grass\grass-7.4.1\etc\python\grass\script\core.py in ()
42 # python3
43 import builtins as builtin
—> 44 from os import environb as environ
45 unicode = str
46 builtin.dict[‘‘] = builtin.dict[’’].self.lgettext

ImportError: cannot import name ‘environb’ from ‘os’ (f:\ProgramData\Anaconda3\lib\os.py)

You are trying to run it in Python 3 which is the first part of the traceback (that is actually fine if you want to run it in Python 3 - but note that the support is experimental).

As for the error itself, environb is a part of os in Python 3, so I don’t understand why that would be missing in your Anaconda Python. You can check the files manually and confirm that all is an expected Python version.

https://docs.python.org/3/library/os.html#os.environb

Thanks, I will check it out Vaclav.
I actually preferred to rewrite the scripts in R and run R from the grass command line.
Thanks for your help. So much dedication to the group of users is really appreciated.

Have a great one,

Mehrdad

···

Mehrdad Varedi