Search code examples
pythoninstallationanacondagdal

GDAL Installation -> ImportError: DLL load failed: The specified procedure could not be found


I am trying to install 'GDAL' python package in conda. Following are the various steps I followed for the installation.

  1. Create a new environment
conda create -n gdal_py37 python=3.7
  1. Activating the new environment
conda activate gdal_py37
  1. Executing requirements file shown here.
pip install -r requirements.txt
  1. From Anaconda Navigator-> environments-> 'gdal_py37' -> select gdal package from uninstalled packages -> Apply

  2. Implemented below line of code in python

import gdal
  1. Following is the error occurring
Output exceeds the size limit. Open the full output data in a text editor
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_18824\1419450745.py in <module>
      7 import os
      8 import sys
----> 9 import gdal
     10 import glob
     11 from tqdm import tqdm

c:\Users\<<User>>\.conda\envs\gdal_py37\lib\site-packages\gdal.py in <module>
      1 # import osgeo.gdal as a convenience
----> 2 from osgeo.gdal import deprecation_warn
      3 deprecation_warn('gdal')
      4 
      5 from osgeo.gdal import *

c:\Users\<<User>>\.conda\envs\gdal_py37\lib\site-packages\osgeo\__init__.py in <module>
     19                 fp.close()
     20             return _mod
---> 21     _gdal = swig_import_helper()
     22     del swig_import_helper
     23 else:

c:\Users\<<User>>\.conda\envs\gdal_py37\lib\site-packages\osgeo\__init__.py in swig_import_helper()
     15         if fp is not None:
...
--> 342         return _load(spec)
    343 
    344 else:

ImportError: DLL load failed: The specified procedure could not be found.

How can I resolve this error?


Solution

  • There is probably a compatibility issue with another package. This can easily happen when you are using conda with pip. Here are two things you should consider:

    1. When using conda you don't want to use pip because it often leads to package incompabilities as pointed out above. Use the conda package manager together with environment.ymlfiles (conda docs | managing environments)
    2. When working with geodata, datascience, etc. in conda, you really should use the conda-forge channel.

    Add the conda-forge channel by running

    conda config --add channels conda-forge
    conda config --set channel_priority strict
    

    If you did not remove your non-functioning environment yet, do it with conda env remove -n gdal_py37.

    Create a file environment.yml which replaces your requirements.txt and should look like this:

    name: gdal_py37
    channels:
      - conda-forge
    dependencies:
      - gdal=3.5.2
      - python=3.7
    

    Create your environment with its dependencies by running:

    conda env create -f environment.yml
    

    Activate your environment with conda activate gdal_py37.

    Now start python and run the following:

    from osgeo import gdal