GeoDjango on Windows: "Could not find the GDAL library" / "OSError: [WinError 126] The specified module could not be found"

I have found the following to work for windows:

  • Run python to check if your python is 32 or 64 bit.
  • Install corresponding OSGeo4W (32 or 64 bit) into C:\OSGeo4W or C:\OSGeo4W64:
    • Note: Select Express Web-GIS Install and click next.
    • In the ‘Select Packages’ list, ensure that GDAL is selected; MapServer and Apache are also enabled by default, may be unchecked safely.
  • Make sure the following is included in your settings.py:

    import os
    if os.name == 'nt':
        import platform
        OSGEO4W = r"C:\OSGeo4W"
        if '64' in platform.architecture()[0]:
            OSGEO4W += "64"
        assert os.path.isdir(OSGEO4W), "Directory does not exist: " + OSGEO4W
        os.environ['OSGEO4W_ROOT'] = OSGEO4W
        os.environ['GDAL_DATA'] = OSGEO4W + r"\share\gdal"
        os.environ['PROJ_LIB'] = OSGEO4W + r"\share\proj"
        os.environ['PATH'] = OSGEO4W + r"\bin;" + os.environ['PATH']
    
  • Run python manage.py check to verify geodjango is working correctly.


After updating some OSGEO4W on my Windows 10 Pro machine I started having problems with the GDAL bindings again. I previously used a combination of the solutions posted here and with this tutorial.

This is what works for me using Windows 10 Pro 64-bit, Django 3.0.6 and GDAL 3.0.4 using a python 3.7 virtual environment. I have tested it without OSGEO4W and it seems to work.

First, download the GDAL wheel from Christoph Gohlke's Unofficial Windows Binaries for Python Extension Packages.

pip install "/path/to/GDAL‑3.0.4‑cp37‑cp37m‑win_amd64.whl"

Modify the libgdal.py file in the virtual envrironment site packages by adding 'gdal300' to line 23 of the Django GDAL package python file (/path/to/virtual_env/Lib/site-packages/django/contrib/gis/gdal/libgdal.py):

elif os.name == 'nt':
    # Windows NT shared libraries
    lib_names = ['gdal300', 'gdal204', 'gdal203', 'gdal202', 'gdal201', 'gdal20']

Finally, in your settings.py file in your Django project add

if os.name == 'nt':
    VENV_BASE = os.environ['VIRTUAL_ENV']
    os.environ['PATH'] = os.path.join(VENV_BASE, 'Lib\\site-packages\\osgeo') + ';' + os.environ['PATH']
    os.environ['PROJ_LIB'] = os.path.join(VENV_BASE, 'Lib\\site-packages\\osgeo\\data\\proj') + ';' + os.environ['PATH']

In my case (Windows10Pro+Python3.7.1), having the (automatically chosen) dll present was not enough, namely gdal111.dll.

I realized that I also had gdal204.dll located at C:\OSGeo4W\bin and tried to "enrich" the list variable named lib_names with 'gdal204', at line 24 (regarding Windows NT shared libraries) of %PYTHON_ROOT%\Lib\site-packages\django\contrib\gis\gdal\libgdal.py, i.e.

#[...]
elif os.name == 'nt':
    # Windows NT shared libraries
    lib_names = ['gdal204', 'gdal202', 'gdal201', 'gdal20', 'gdal111', 'gdal110', 'gdal19']
#[...]            ^^^^^^^

No negative consequences for now.