GDAL and its Python 3 bindings have been installed through apt package manager for weeks, but, suddenly:

from osgeo import GDAL
ImportError                               Traceback (most recent call last)
<ipython-input-1-61e0eefce4de> in <module>()
----> 1 from osgeo import gdal

/usr/lib/python3/dist-packages/osgeo/__init__.py in <module>()
     19                 fp.close()
     20             return _mod
---> 21     _gdal = swig_import_helper()
     22     del swig_import_helper
     23 else:

/usr/lib/python3/dist-packages/osgeo/__init__.py in swig_import_helper()
     15         if fp is not None:
     16             try:
---> 17                 _mod = imp.load_module('_gdal', fp, pathname, description)
     18             finally:
     19                 fp.close()

/usr/local/pythonenv/smap_l4c-env/lib/python3.6/imp.py in load_module(name, file, filename, details)
    241                 return load_dynamic(name, filename, opened_file)
    242         else:
--> 243             return load_dynamic(name, filename, file)
    244     elif type_ == PKG_DIRECTORY:
    245         return load_package(name, filename)

/usr/local/pythonenv/smap_l4c-env/lib/python3.6/imp.py in load_dynamic(name, path, file)
    341         spec = importlib.machinery.ModuleSpec(
    342             name=name, loader=loader, origin=path)
--> 343         return _load(spec)
    345 else:

ImportError: /usr/lib/libgdal.so.20: undefined symbol: opj_codec_set_threads

The same error, with slightly different Traceback, is obtained with import gdal.

I didn't recognize this symbol name, opj_codec_set_threads so I looked it up online. It's apparently part of the OpenJPEG library. I recalled that I had recently run some legacy setup script for another project with OpenJPEG as a dependency. That legacy setup script apparently installed an older verison of OpenJPEG (v2.1.0) on top of what apt had installed (v2.3.0) when I first setup the machine.

I tried reinstalling OpenJPEG from apt:

sudo apt install --reinstall libopenjp2-7 libopenjp2-7-dev
sudo ldconfig

This did not solve the problem. I tried re-installing GDAL and its bindings:

sudo apt install --reinstall gdal-bin libgdal-dev python-gdal python3-gdal

Still no good.


The older version of OpenJPEG was installed with cmake. I gathered that GDAL's shared objects from OpenJPEG (expecting v2.3.0) were misplaced since the install but it wasn't initially obvious where the v2.1.0 files were, how to remove, or update the links.

Luckily, cmake creates a file called install_manifest.txt to keep track of its installed files. I finally used cmake to uninstall v2.1.0:

cd /my/openjpeg-2.1.0/install/dir
xargs rm < install_manifest.txt

Then, I was able to reinstall OpenJPEG, GDAL, and its Python bindings.

sudo apt install --reinstall libopenjp2-7 libopenjp2-7-dev
sudo apt install --reinstall gdal-bin libgdal-dev python-gdal python3-gdal

Now I can import gdal without any problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.