8

I just did a fresh install of Anaconda3 and am trying to add GDAL/OGR. I've tried installing with both conda install gdal and conda install -c conda-forge gdal. In both cases, the install completes successfully, but when I import gdal, it raises the error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Jhook\AppData\Local\Continuum\miniconda3\lib\site-packages\gdal.py", line 2, in <module>
    from osgeo.gdal import deprecation_warn
  File "C:\Users\Jhook\AppData\Local\Continuum\miniconda3\lib\site-packages\osgeo\__init__.py", line 21, in <module>
    _gdal = swig_import_helper()
  File "C:\Users\Jhook\AppData\Local\Continuum\miniconda3\lib\site-packages\osgeo\__init__.py", line 17, in swig_import_helper
    _mod = imp.load_module('_gdal', fp, pathname, description)
  File "C:\Users\Jhook\AppData\Local\Continuum\miniconda3\lib\imp.py", line 243, in load_module
    return load_dynamic(name, filename, file)
  File "C:\Users\Jhook\AppData\Local\Continuum\miniconda3\lib\imp.py", line 343, in load_dynamic
    return _load(spec)
ImportError: DLL load failed: The specified module could not be found.

I've found several workarounds out there, but most date back a few years and don't reflect more recent updates or changes. Is there a fix for this problem that works in 2018?

6
  • What OS are you running this on? How did you call import gdal, via a terminal or an IDE?
    – Aaron
    Aug 28, 2018 at 15:16
  • Running on Windows 10. Result is the same for import gdal from a terminal and from PyCharm.
    – triphook
    Aug 28, 2018 at 15:19
  • 1
    Try ‘from osgeo import gdal’
    – Aaron
    Aug 28, 2018 at 16:58
  • 1
    from osgeo import gdal results in the same message
    – triphook
    Aug 28, 2018 at 17:28
  • Can you please confirm you are running Python 3?
    – Aaron
    Aug 28, 2018 at 18:06

4 Answers 4

10

There seems to be an incompatibility between the latest version of conda and vs2015_runtime and conda-forge gdal.

If I create a conda-forge env and specify vs2015_runtime=14, everything works:

conda create -n testgdal -c conda-forge gdal vs2015_runtime=14 

activate testgdal 

(testgdal) python 

Python 3.6.6 | packaged by conda-forge | (default, Jul 26 2018, 11:48:23) [MSC v.1900 64 bit (AMD64)] on win32 
Type "help", "copyright", "credits" or "license" for more information.  

>>> from osgeo import gdal
>>>

https://gis.stackexchange.com/a/291932

2
  • Thanks for this. Was going crazy for a while. Shouldn't this get done automatically by anaconda?
    – user32882
    Jun 26, 2019 at 9:41
  • It should, but conda-forge is not Anaconda. It's a separate community led channel and occasionally things get out of sync
    – user2856
    Jun 26, 2019 at 9:44
1

I've spent the day with that issue, and for me what worked was to install from wheels, as detailed on that video: https://www.youtube.com/watch?v=4ybddFC80fU

It is pretty simple. The only thing I had to change was to also install Fiona from wheel first, otherwise I had an error saying "a GDAL API version must be specified...[]"

1
  • Could you write what the steps are from that video? I think it would make your answer more helpful and more accessible.
    – jvriesem
    Nov 2, 2021 at 17:31
1

The following works reliably for me Ubuntu 20.04:

conda update conda    
sudo apt-get install libgdal-dev gdal-bin

export CPLUS_INCLUDE_PATH=/usr/include/gdal
export C_INCLUDE_PATH=/usr/include/gdal

# check gdal version with 
gdal-config --version

echo '
name: raster-pipeline
channels:
- defaults
dependencies:
- python=3.7.*
- cython
- numpy
- ipython
- libspatialindex
- libgdal=USE_GDAL_VERSION
- gdal=USE_GDAL_VERSION
- pip
- pip:
  - numpy>=1.18.5
  - GDAL==USE_GDAL_VERSION
  - pyproj>=2.6.1.post1
  - rasterio>=1.1.5
' > raster_pipeline.yml
  
conda env create -f raster_pipeline.yml -v 
 
conda activate raster_pipeline 
python -c "from osgeo import gdal"
conda deactivate 
1

Like everyone said there seems to be an incompatibility issue. Creating a new environment solved the problem. This worked for me

Go to anaconda navigator --> environments --> Create --> GDAL Go to anaconda prompt --> activate GDAL --> conda install gdal

Hope this will do. But you have to install other libraries again in this new environment.

Your Answer

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

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