3

I have a conda environment and install gdal into it as suggested here https://gdal.org/api/python_bindings.html with the following command:

conda install -c conda-forge gdal

However, when I try to import the modul gdal with the command

from osgeo import gdal

I get the Error:

ModuleNotFoundError: No module named '_gdal'

What am I doing wrong, and how do I need to properly install gdal? I'm using Linux Mint.

6
  • I tried it with conda install gdal and only this seems to work.
    – i.i.k.
    Commented May 19 at 15:26
  • 1
    Are the packages still installed from conda-forge? Check with conda list. conda install -c conda-forge gdal works for me on Windows. I have Miniconda v. 23.3.1. Python 3.12.3 got installed.
    – user30184
    Commented May 19 at 15:34
  • Yes, gdal is installed with conda-forge according to the list.
    – i.i.k.
    Commented May 19 at 15:38
  • I think you can be happy then if you do not definitely want to know why the documented command did not work for you.
    – user30184
    Commented May 19 at 15:47
  • If you want to explore further on the conda-forge channel install, you can try: conda install -c conda-forge gdal krb5 to see if that solves the issue. Commented May 19 at 16:16

1 Answer 1

4

When using conda it is in general a good idea to avoid mixing packages from different channels.

Anaconda and miniconda use defaults as default channel, so when you use conda install -c conda-forge gdal only gdal will be installed from the conda-forge channel and most other dependencies will be installed from defaults, possibly leading to incompatibilities.

So, when you want to install something explicitly from the conda-forge channel, it is a good idea to create a new environment and configure it so the highest priority is given to conda forge for everything being installed in this environment, like this:

conda create -n gdal_cf
conda activate gdal_cf
conda config --env --add channels conda-forge
conda config --env --set channel_priority strict
conda install gdal

This should avoid this kind of issues.

Reference: here.

Regarding conda install gdal working: this will work as well as it will install gdal as well as all dependencies from defaults. However, the latest version on defaults is 3.6.2, so it trails 3 feature versions as 3.9 is the latest version (which is available on conda-forge).

3
  • 2
    Notice also that if you happen to work for a bigger company (>200 people) then the default conda channel is not free, while conda-forge is anaconda.com/blog/is-conda-free.
    – user30184
    Commented May 19 at 19:16
  • @user30184 This is indeed a very interesting fact. Didn't know that! Thank you for sharing!
    – i.i.k.
    Commented May 20 at 10:47
  • 1
    Thank you @Pieter! Very helpful!
    – i.i.k.
    Commented May 20 at 10:54

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.