Skip to content

Commit

Permalink
Doc building: import GDAL Python bindings early
Browse files Browse the repository at this point in the history
We don't strictly need to import them at the beginning of conf.py, but
importing them early helps in having a better error message if they
cannot be imported rather than waiting later when autodoc tries to load them

In my case, this was due to an obscure situation. My libgdal.so in my
build tree links to a /path/to/install-libjpeg-turbo/lib/libjpeg.so.8, with
it being libjpeg-turbo 3.0 with new symbols. That full path to libjpeg is
embedded in libgdal.so, but /path/to/install-libjpeg-turbo/lib was not
pointed in LD_LIBRARY_PATH.
This worked fine when doing "from osgeo import gdal" from a 'regular'
Python interpreter, or running pytest. But strangely, this failed within the
sphinx-build process. The autodoc error message wasn't helpful just
saying it couldn't import the osgeo module, whereas if trying to import
in conf.py I got an exception pointing to the fact a symbol from
libjpeg.so couldn't be loaded. I suspect that something in the
sphinx-build process loads the system libjpeg library, that does not
have the new symbols of libjpeg-turbo 3.0 that my libgdal.so was using,
hence causing later loading of libgdal to fail.

Now that I've added /path/to/install-libjpeg-turbo/lib in
LD_LIBRARY_PATH, things work and this patch isn't actually needed, but
it may help diagnose further similar situations
  • Loading branch information
rouault committed Apr 26, 2024
1 parent 512f272 commit 2be3c80
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@

sys.path.insert(0, os.path.abspath("_extensions"))

if "IGNORE_MISSING_GDAL_PYTHON_BINDINGS" not in os.environ:
try:
# We don't need them right now, but importing them early helps in having
# a better error message if they cannot be imported, rather than waiting
# later when autodoc tries to load them and may fail with less details.
from osgeo import gdal # NOQA
except ImportError as e:
raise Exception(
"The GDAL Python bindings must be accessible to build the documentation.\n"
"Cf https://gdal.org/development/dev_documentation.html#python-api-documentation.\n"
"You may suppress this check by setting the IGNORE_MISSING_GDAL_PYTHON_BINDINGS=YES environment variable (but Python API docs will not be built)"
).with_traceback(e.__traceback__)

# -- Project information -----------------------------------------------------

Expand Down

0 comments on commit 2be3c80

Please sign in to comment.