[GRASS-user] Using grass libraries in python outside of GRASS

Hi,

I may be missing something very simple here. Is there a way to access the GRASS python libraries outside of the GRASS GUI without calling python through GRASS. I built a python environment (in Anaconda) that is setup with other libraries that are required for a model I need to run. I would like to be able to call the GRASS libraries in that custom environment.

Thanks.

Ellen

Ellen D’Amico, GISP

GIS Specialist
Pegasus Technical Services c/o US EPA
26 W. Martin Luther King Dr.
Cincinnati, OH 45268
513-569-7167

Hi Ellen,

On Tue, Jul 28, 2020 at 8:49 PM DAmico, Ellen <DAmico.Ellen@epa.gov> wrote:

Hi,

I may be missing something very simple here. Is there a way to access the GRASS python libraries outside of the GRASS GUI without calling python through GRASS. I built a python environment (in Anaconda) that is setup with other libraries that are required for a model I need to run. I would like to be able to call the GRASS libraries in that custom environment.

Please take a look at

Python: GRASS GIS 7 with an external library: grass-session
https://grasswiki.osgeo.org/wiki/Working_with_GRASS_without_starting_it_explicitly#Python:_GRASS_GIS_7_with_an_external_library:_grass-session

This is probably the easiest way.

Best,
Markus

--
Markus Neteler, PhD
https://www.mundialis.de - free data with free software
https://grass.osgeo.org
https://courses.neteler.org/blog

(please keep the list in cc)

On Tue, Jul 28, 2020 at 9:17 PM DAmico, Ellen <DAmico.Ellen@epa.gov> wrote:

Thanks for the quick response. Does that work with both python 2.7 and 3?

Honestly, I don't know but you may want to verify here:

https://github.com/zarch/grass-session

The model I need to use (which I did not write) requires python 2.7.
I had tried pip install previously but then it still fails with ImportError: No module named grass.script.
Do you know what could be causing that?

Perhaps simply a PYTHONPATH issue?
At the bottom of this page there are some examples:

https://github.com/zarch/grass-session/blob/master/README.rst

Markus

Thanks. Those links go to the same place are they supposed to? I will take a deeper look tomorrow. I appreciate your help.

-----Original Message-----
From: Markus Neteler <neteler@osgeo.org>
Sent: Tuesday, July 28, 2020 4:28 PM
To: DAmico, Ellen <DAmico.Ellen@epa.gov>
Cc: GRASS user list <grass-user@lists.osgeo.org>
Subject: Re: [GRASS-user] Using grass libraries in python outside of GRASS

(please keep the list in cc)

On Tue, Jul 28, 2020 at 9:17 PM DAmico, Ellen <DAmico.Ellen@epa.gov> wrote:

Thanks for the quick response. Does that work with both python 2.7 and 3?

Honestly, I don't know but you may want to verify here:

https://gcc01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fzarch%2Fgrass-session&amp;data=02|01|DAmico.Ellen%40epa.gov|9388b82c13c34de0343308d83334bb71|88b378b367484867acf976aacbeca6a7|0|0|637315648877416940&amp;sdata=CLnQvLXXt5zuqSCAUj6T4d3TnmhdTAbmYj10e%2B%2BiwJ4%3D&amp;reserved=0

The model I need to use (which I did not write) requires python 2.7.
I had tried pip install previously but then it still fails with ImportError: No module named grass.script.
Do you know what could be causing that?

Perhaps simply a PYTHONPATH issue?
At the bottom of this page there are some examples:

https://gcc01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fzarch%2Fgrass-session%2Fblob%2Fmaster%2FREADME.rst&amp;data=02|01|DAmico.Ellen%40epa.gov|9388b82c13c34de0343308d83334bb71|88b378b367484867acf976aacbeca6a7|0|0|637315648877416940&amp;sdata=Js%2BAvHd8J65XPqEl3EDfWF9fIq%2F3UZJHG3S9KhC4NLg%3D&amp;reserved=0

Markus

On Tue, Jul 28, 2020 at 10:27 PM Markus Neteler <neteler@osgeo.org> wrote:

On Tue, Jul 28, 2020 at 9:17 PM DAmico, Ellen <DAmico.Ellen@epa.gov> wrote:

...

> The model I need to use (which I did not write) requires python 2.7.
> I had tried pip install previously but then it still fails with ImportError: No module named grass.script.
> Do you know what could be causing that?

Interestingly, our docker containers just started to suffer from a
similar (same?) problem:

File "/scripts/test_grass_session.py", line 3, in <module>
import grass.script as grass
ModuleNotFoundError: No module named 'grass'

Weird, we need to investigate.

Markus

Perhaps simply a PYTHONPATH issue?
At the bottom of this page there are some examples:

https://github.com/zarch/grass-session/blob/master/README.rst

Markus

--
Markus Neteler, PhD
https://www.mundialis.de - free data with free software
https://grass.osgeo.org
https://courses.neteler.org/blog

Interesting. I will await anything you find. Thank you. Ellen

-----Original Message-----
From: Markus Neteler <neteler@osgeo.org>
Sent: Wednesday, July 29, 2020 4:01 AM
To: DAmico, Ellen <DAmico.Ellen@epa.gov>
Cc: GRASS user list <grass-user@lists.osgeo.org>
Subject: Re: [GRASS-user] Using grass libraries in python outside of GRASS

On Tue, Jul 28, 2020 at 10:27 PM Markus Neteler <neteler@osgeo.org> wrote:

On Tue, Jul 28, 2020 at 9:17 PM DAmico, Ellen <DAmico.Ellen@epa.gov> wrote:

...

> The model I need to use (which I did not write) requires python 2.7.
> I had tried pip install previously but then it still fails with ImportError: No module named grass.script.
> Do you know what could be causing that?

Interestingly, our docker containers just started to suffer from a similar (same?) problem:

File "/scripts/test_grass_session.py", line 3, in <module> import grass.script as grass
ModuleNotFoundError: No module named 'grass'

Weird, we need to investigate.

Markus

Perhaps simply a PYTHONPATH issue?
At the bottom of this page there are some examples:

https://gcc01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgith
ub.com%2Fzarch%2Fgrass-session%2Fblob%2Fmaster%2FREADME.rst&amp;data=0
2%7C01%7CDAmico.Ellen%40epa.gov%7C6e81e6c4acc84db1340008d833959e0f%7C8
8b378b367484867acf976aacbeca6a7%7C0%7C0%7C637316064983993601&amp;sdata
=bPE1zELFkkj3HpVVopBH7a6nPpAdRK%2BdC2Tj1h4hdIs%3D&amp;reserved=0

Markus

--
Markus Neteler, PhD
https://gcc01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.mundialis.de%2F&amp;data=02|01|DAmico.Ellen%40epa.gov|6e81e6c4acc84db1340008d833959e0f|88b378b367484867acf976aacbeca6a7|0|0|637316064983993601&amp;sdata=I6CFUA2PPoaFwdRImG24FuTsIUsOuqwtzxF%2Fy3nV0B8%3D&amp;reserved=0 - free data with free software
https://gcc01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgrass.osgeo.org%2F&amp;data=02|01|DAmico.Ellen%40epa.gov|6e81e6c4acc84db1340008d833959e0f|88b378b367484867acf976aacbeca6a7|0|0|637316064983993601&amp;sdata=gnL1xIhAsTEfJznQqT%2BMUkVYBPXZArIMFvz5Y%2FeLM2U%3D&amp;reserved=0
https://gcc01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fcourses.neteler.org%2Fblog&amp;data=02|01|DAmico.Ellen%40epa.gov|6e81e6c4acc84db1340008d833959e0f|88b378b367484867acf976aacbeca6a7|0|0|637316064983993601&amp;sdata=X6lnIHt7PAJEc2pO0mixaTNicoCBBcoTJdezpJ%2BfFTg%3D&amp;reserved=0

Hi

The model I need to use (which I did not write) requires python 2.7.
I had tried pip install previously but then it still fails with ImportError: No module named grass.script.
Do you know what could be causing that?

Interestingly, our docker containers just started to suffer from a
similar (same?) problem:

File “/scripts/test_grass_session.py”, line 3, in
import grass.script as grass
ModuleNotFoundError: No module named ‘grass’

Weird, we need to investigate.

Grass_session works for me with both python2.7 and 3 in a Osgeo environment with grass 7.8.3
For me, the import modules work in this order in both python versions after pip installation.

>>> import grass_session
>>> from grass_session import Session

>>> import grass.script as grass

For the final script to work, you will have to set environment variables, GISBIN (grass78 root folder) , GISRC (.grassrc78 file in demo location) and add grass78 ‘lib’, ‘bin’ and ‘scripts’ folders to PATH variable.

I run the script in python 3, where it worked without setting GISBIN and GISRC variables. I have only tested this approach in Windows 10 yet, and should work fine in Linux as well.

Regards

Sajid

Thank you that solved it with some tweaks.

I added GRASSBIN to my Environment variables populated with the grass bat file instead of GISBIN. Then added ‘lib’, ‘bin’ and ‘scripts’ folders to PATH variable. Once I did that that I was able to import grass_session and grass.

It appears to work fine without GISRC set.

Ellen

···

Hi

The model I need to use (which I did not write) requires python 2.7.
I had tried pip install previously but then it still fails with ImportError: No module named grass.script.
Do you know what could be causing that?

Interestingly, our docker containers just started to suffer from a
similar (same?) problem:

File “/scripts/test_grass_session.py”, line 3, in
import grass.script as grass
ModuleNotFoundError: No module named ‘grass’

Weird, we need to investigate.

Grass_session works for me with both python2.7 and 3 in a Osgeo environment with grass 7.8.3

For me, the import modules work in this order in both python versions after pip installation.

>>> import grass_session
>>> from grass_session import Session

>>> import grass.script as grass

For the final script to work, you will have to set environment variables, GISBIN (grass78 root folder) , GISRC (.grassrc78 file in demo location) and add grass78 ‘lib’, ‘bin’ and ‘scripts’ folders to PATH variable.

I run the script in python 3, where it worked without setting GISBIN and GISRC variables. I have only tested this approach in Windows 10 yet, and should work fine in Linux as well.

Regards

Sajid