Skip to content

grass.script: Load C libraries into the process in init to make grass.lib work - #7733

Open
petrasovaa wants to merge 2 commits into
OSGeo:mainfrom
petrasovaa:preload-dynamic-libraries
Open

grass.script: Load C libraries into the process in init to make grass.lib work#7733
petrasovaa wants to merge 2 commits into
OSGeo:mainfrom
petrasovaa:preload-dynamic-libraries

Conversation

@petrasovaa

Copy link
Copy Markdown
Contributor

Importing grass.lib (ctypes interface to the C libraries) fails with ImportError: Could not load grass_gis... in a Python process that set up a GRASS session with gj.init() or gs.setup.init(), e.g., in a Jupyter notebook. The session setup sets the dynamic library search path variable (LD_LIBRARY_PATH on Linux), but the dynamic linker reads it only at process startup, so in the already running process the GRASS libraries cannot resolve their dependencies on each other (unless the libraries' RUNPATH, i.e. the configured install prefix, happens to be valid).

The library preloading approach is borrowed from the grass-session package (load_libs()), with a few differences:

  • Loads only libgrass_* libraries (grass-session loads every *.so in the directory) and also globs *.dylib, so it works on macOS builds where grass-session's .so-only pattern finds nothing.
  • Skips Windows entirely: there the ctypes loader already registers directories from PATH via os.add_dll_directory() when grass.lib is imported, which happens after init has extended PATH, so preloading is not needed.
  • Failures are silently skipped instead of raising: a library that cannot load (e.g., missing optional system dependency like OpenGL) should not break init for users who never import it, and its grass.lib import fails the same way with or without preloading.
  • The retry loop builds a new list each pass and stops when a pass makes no progress, avoiding grass-session's mutation of the list while iterating over it.
  • No mode=1 (RTLD_LAZY) argument: ctypes always adds RTLD_NOW on POSIX, so the flag has no effect; the default mode matches what the generated grass.lib loader uses.

@petrasovaa
petrasovaa requested a review from wenzeslaus July 16, 2026 12:52
@github-actions github-actions Bot added Python Related code is in Python libraries tests Related to Test Suite labels Jul 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libraries Python Related code is in Python tests Related to Test Suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants