1

I know that this has been asked multiple times, but I still arrive at the ModuleNotFoundError: No module named '_gdal' error in my Ubuntu 20.04 workstation and with Python 3.8 under Anaconda. The following are suggestions that I have tried:

  1. Install GDAL with conda install -c conda-forge gdal - it seemed to successfully install at first, but upon importing osgeo into Python, I get the ModuleNotFoundError;
  2. Install GDAL through pip - this does not succeed in installing GDAL. I receive an error indicating that the wheel file cannot be built;
  3. Install pygdal either through pip or conda (https://gis.stackexchange.com/a/356609/208986) - this has the same result as (1). I even downgraded my Python to 3.6 to install it through conda, but to no success;
  4. https://stackoverflow.com/a/72413306/14081094 - I cannot successfully complete these steps due to GDAL not installing through pip (see 2);
  5. https://stackoverflow.com/a/72887401/14081094, https://stackoverflow.com/a/41613466/14081094 - it was able to successfully install GDAL in Ubuntu 20.04, but unable to install GDAL in Python, again, through pip or conda (see 1 and 2);

Weirdly enough, I can import GDAL through the base environment of Anaconda with no errors (using any of the steps), however when I try to import it through a virtual environment, the error occurs.

2
  • 3
    Did you perhaps use import gdal instead of from osgeo import gdal?
    – user30184
    Jun 9, 2023 at 5:45
  • I am importing GDAL through from osgeo import gdal.
    – magus_e
    Jun 9, 2023 at 10:34

4 Answers 4

3

Works for me on Windows with the commands copied from https://gist.github.com/ThomasG77/40a9f033131c9b49fe70b22a5f9c701a

conda create --name gdaltest python=3.8
conda activate gdaltest
conda install gdal libgdal

python
>>> from osgeo import gdal
>>> gdal.VersionInfo()
'3060200'

Article https://opensourceoptions.com/blog/how-to-install-gdal-with-anaconda/ has advice for troubleshooting.

EDIT

It may be necessary to install also krb5, see https://gis.stackexchange.com/a/440118

conda install krb5
2
  • It worked! Thank you very much!
    – magus_e
    Jun 9, 2023 at 19:01
  • I personally prefer to specify all required packages when creating the environment instead of afterwards, e.g. conda create --name gdaltest python=3.8 gdal libgdal etc as conda only has to 'solve' the environment once and you're less likely to get any inconsistent packages and have to upgrade/downgrade existing packages.
    – user2856
    Jun 9, 2023 at 22:18
0

Not the best solution, but suffices for me on a Mac M1: pinning gdal<3.6 in my Conda environment made things work for me again.

After some CONDA_SUBDIR=osx-64 micromamba create --prefix ./venv -f conda_environment.yml I then see _gdal.py exists:

ls -l ./venv/**/_gdal.py
-rw-rw-r--  2 arjan  staff  420 Sep  1  2021 ./venv/lib/python3.8/site-packages/osgeo/_gdal.py

Without that pin, which in my project got me GDAL 3.6.x today (which is not the latest, probably due to a Python 3.8 dependency in my project), I got the ModuleNotFoundError: No module named '_gdal' and no file _gdal.py exists then. That’s all I know; hoping it may help someone investigating for a proper solution if things suddenly break.

0

This method works for me in python 3.11 First, download the appropriate .whl file (in my case GDAL-3.4.3-cp311-cp311-win_amd64.whl since I use python 3.11) from

https://www.lfd.uci.edu/~gohlke/pythonlibs/#gdal

then, install the file from command prompt, as can be seen in

https://opensourceoptions.com/how-to-install-gdal-for-python-with-pip-on-windows/

0

I had a similar situation (on Mac). While ogrmerge.py was complaining about ModuleNotFoundError: No module named '_gdal', ogr2ogr returned:

dyld[56812]: Library not loaded: /opt/homebrew/opt/x265/lib/libx265.199.dylib
  Referenced from: <6162BB8E-91FB-38CF-98D7-5C1E2DEE6E55> /opt/homebrew/Cellar/libheif/1.17.6/lib/libheif.1.17.6.dylib
  Reason: tried: '/opt/homebrew/opt/x265/lib/libx265.199.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/opt/x265/lib/libx265.199.dylib' (no such file), '/opt/homebrew/opt/x265/lib/libx265.199.dylib' (no such file), '/usr/local/lib/libx265.199.dylib' (no such file), '/usr/lib/libx265.199.dylib' (no such file, not in dyld cache), '/opt/homebrew/Cellar/x265/3.6/lib/libx265.199.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/Cellar/x265/3.6/lib/libx265.199.dylib' (no such file), '/opt/homebrew/Cellar/x265/3.6/lib/libx265.199.dylib' (no such file), '/usr/local/lib/libx265.199.dylib' (no such file), '/usr/lib/libx265.199.dylib' (no such file, not in dyld cache)
    zsh: abort      ogr2ogr

Got both fixed by:

brew reinstall libheif

Based on this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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