2

I have been trying to use pygrass from grass78 with Python 3.7 inside a Jupyter notebook and have run into an issue where I can import grass modules, but cannot call any pygrass functions, e.g, r.in_gdal.

I first used this wiki to install grass78 (debian stretch section): https://grasswiki.osgeo.org/wiki/Compile_and_Install#GRASS_7_on_Debian_Stretch

And then this wiki: https://grasswiki.osgeo.org/wiki/GRASS_GIS_Jupyter_notebooks to set up my environment. I am trying to use pygrass within a jupyter notebook within a docker container and so I configured all the "session" environmental variables with my Dockerfile.

When I open a notebook to check my environmental variables I run the following:

print(f"GISBASE is: {os.environ['GISBASE']}")
print(f"MYGISDBASE is: {os.environ['MYGISBASE']}")
print(f"LOCATION is: {os.environ['MYLOC']}")
print(f"MAPSET is: {os.environ['MYMAPSET']}")
print(f"LD_LIBRARY_PATH is: {os.environ['LD_LIBRARY_PATH']}")
print(f"PYTHONPATH is: {os.environ['PYTHONPATH']}")
print(f"PATH is: {os.environ['PATH']}")

and see the following:

GISBASE is: /usr/local/grass78/
MYGISDBASE is: /root/jupyterhub-workspace/grassdata
LOCATION is: Pacific
MAPSET is: PERMANENT
LD_LIBRARY_PATH is: /usr/local/grass78/lib:/usr/local/grass78/lib
PYTHONPATH is: /usr/local/grass78/etc/python:/usr/local/grass78/etc/python
PATH is: /root/.local/bin:/opt/conda/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/grass78/bin:/usr/local/grass78/scripts:/root/.grass7/addons/scripts:/root/.grass7/addons/bin

Per this example (landsat 7 section): https://grasswiki.osgeo.org/wiki/Python/pygrass, I run the following:

from grass import script as g
from grass.script import setup as gsetup

import os
gisbase = os.environ['GISBASE']
dbase = os.environ['MYGISBASE']
location = os.environ['MYLOC']
mapset = os.environ['MYMAPSET']

gsetup.init(gisbase,dbase,location,mapset)

This code raises no errors and writes a temp config file that looks like this to my tmp:

enter image description here

I proceed to import pygrass packages:

from grass.pygrass.modules.shortcuts import raster as r
from grass.pygrass.modules.shortcuts import imagery as i
from grass.pygrass.modules.shortcuts import display as d
from grass.pygrass.modules import Module

which import with no apparent problems.

However, when I then call:

r.in_gdal(input='ned_NED_13.tif',output='test',flags="e",overwrite=True)

Meaning to take a tif in my home directory and read it into a raster map called 'test', I get this error:

enter image description here

In an effort to debug the error, I ran this:

Module('r.in.gdal')

with the same result.

When I then ran:

from grass import script
script.run_command('r.in.gdal')

I got a different error, but am unsure how to access the error message:

enter image description here

Does anyone have know why I see this error behavior?

1
  • To clarify, when I try to run grass from the terminal of my docker container, I cannot do that either.
    – SolarJoe
    Nov 13 '19 at 19:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.