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.

magus_e
  • 15
  • 1
  • 5

3 Answers3

2

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
user30184
  • 65,331
  • 4
  • 65
  • 118
  • It worked! Thank you very much! – magus_e Jun 09 '23 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 09 '23 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.

Arjan
  • 211
  • 1
  • 10
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/

Vince
  • 20,017
  • 15
  • 45
  • 64
HKF
  • 1