[GRASS-git] [OSGeo/grass] efc067: init: Simply import grass, try harder if it fails ...
Vaclav Petras
noreply at github.com
Mon Sep 29 09:43:20 PDT 2025
Branch: refs/heads/main
Home: https://github.com/OSGeo/grass
Commit: efc0677caaa113e06709fb95ed1217dc5d8e8ff7
https://github.com/OSGeo/grass/commit/efc0677caaa113e06709fb95ed1217dc5d8e8ff7
Author: Vaclav Petras <wenzeslaus at gmail.com>
Date: 2025-09-29 (Mon, 29 Sep 2025)
Changed paths:
M lib/init/grass.py
Log Message:
-----------
init: Simply import grass, try harder if it fails (#6393)
Previously, the path with the package was required to exist (both the hardcoded etc/python under GISBASE and FHS variable one). Now, the code first attempts the import and only when the import fails, it tries to work out the path. The simple import should work when FHS is actually used and the grass package is where Python looks for packages (so no GRASS_PYDIR is needed there). It will also work when a user sets up PYTHONPATH manually expecting it will work the same as when setting it before running Python and importing the grass package there (both PYTHONPATH and GRASS_PYDIR will work, but PYTHONPATH will user the Python native mechanics to make the import work, while GRASS_PYDIR needs to use sys.path.append). If GRASS_PYDIR does not work (either the env variable or the one from the build), the code will attempt to find the GISBASE directory relative to the main executable and expects a non-FHS layout (which is reasonable since import should work without any setup with FHS).
Prepend, not append to sys.path. Remove sys.path manipulations from the rest of grass.py file because the smarter code to do that is already executed by this point.
* Try to import more specific grass.script, not just general grass to avoid having the import work for grass.py (issue on Windows where .py is not removed). If there is something which was imported as grass (grass.py would be the case), but does not have script, we need to convince Python to repeat the import of grass. We do that by removing it from sys.modules cache. This gives us a fresh start for next import with new path at the beginning of sys.path. Use a more specific exception type (available since Python 3.6).
* Apply suggestions from code review
---------
Co-authored-by: Anna Petrasova <kratochanna at gmail.com>
To unsubscribe from these emails, change your notification settings at https://github.com/OSGeo/grass/settings/notifications
More information about the grass-commit
mailing list