Skip to content

[Feat] grass package should be available in Python after install #658

@wenzeslaus

Description

@wenzeslaus

Is your feature request related to a problem? Please describe.

On Linux, the grass package is not available in the system Python (i.e. not on path) after I install GRASS GIS into the system.

$ sudo apt install grass
$ python3
>>> import grass
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'grass'

Describe the solution you'd like

After installing GRASS GIS to my system, I would like the grass package to be available without having to put it to the the path manually.

For example on Ubuntu, I would like to use the following:

$ sudo apt install grass
$ python3
>>> import grass

This means that grass package would have to be in /usr/lib/python3/dist-packages where you can find, for example, the qgis package.

This is just how other things behave: GDAL (from osgeo import gdal), QGIS (if you are lucky enough with dynamic libraries), Python packages in general, ... In R I can import/load the bindings without a prior setup. Overall, this goes against user expectations. It is true that many of these are internally very different than GRASS GIS, but that's something users should not worry about.

Describe alternatives you've considered

Modify sys.path or PYTHONPATH

The current way is modifying sys.path, but for that I first need to find out where GRASS GIS lives (manually or programmatically):

import os
import sys
import subprocess
gisbase = subprocess.check_output(["grass", "--config", "path"]).strip()
sys.path.append(os.path.join(gisbase, "etc", "python"))
import grass

If you are linting your code you will need to add also at least one of:

# noqa: E402
# pylint: disable=g-import-not-at-top

(To make it actually work, you will likely need to add also os.environ['GISBASE'] = gisbase, but that's a separate issue.)

Use grass-session

The following looks much better:

from grass_session import Session
import grass

However, I need to use two different packages, get the import order right, and most importantly, I need to install it from a completely different source. Additionally - and that may or may not be fixed by using dist-packages - grass_session/grass-session is not linked with the installation, so I may end up setting up GRASSBIN for it beforehand anyway so not that different from using PYTHONPATH.

Import from GRASS GIS

I can run the script inside GRASS GIS, but then I need to get GRASS GIS running somehow first, so I'm not using GRASS GIS from Python anymore, but more the other way around. This is great for writing GRASS GIS modules, but not for writing scripts and using GRASS GIS in them. It is also more difficult to apply this approach for running within some other environment such as JupyterLab or a Python IDE.

Using --exec from Python

I could use GRASS GIS through command line interface as in grass ... --exec, but I'm throwing away the useful grass.script functionality and I will eventually end up re-implementing it again just outside of GRASS GIS.

Additional context

As far as I understand, this would bring Linux installations to where the Windows one is now (cbc7826, example).

Optionally, this can be the time to re-consider the name of the package (I used grass above), inclusion of GUI code, and placement of these in the source code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestlinuxLinux specific

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions