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

82👍

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.

👤Udi

33👍

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']

7👍

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.

3👍

For Microsoft Windows 10 & Python3.6.8, I installed GDAL 2.3.3 from Unofficial Windows Binaries for Python Extension Packages, modified libgdal.py adding gdal203 in the lib_names list env\Lib\site-packages\django\contrib\gis\gdal\libgdal.py.

Finally, added osgeo and proj to the PATH, and set the GDAL_LIBRARY_PATH as below (beginning of settings.py):

os.environ['PATH'] = os.path.join(BASE_DIR, r'env\Lib\site-packages\osgeo') + ';' + os.environ['PATH']

os.environ['PROJ_LIB'] = os.path.join(BASE_DIR, r'env3\Lib\site-packages\osgeo\data\proj') + ';' + os.environ['PATH']

GDAL_LIBRARY_PATH = os.path.join(BASE_DIR, r'env\Lib\site-packages\osgeo\gdal203.dll') 

In this case, env is my Python environment.

1👍

Steps to follow:

  1. Run python to check if your python is 32 or 64 bit.
  2. Install corresponding OSGeo4W (32 or 64 bit) into C:\OSGeo4W or C:\OSGeo4W64:
    Note: Select Express Web-GIS Install and click next.
  3. In the ‘Select Packages’ list, ensure that GDAL is selected; MapServer and Apache are also enabled by default.
  4. Make sure the following is included in your settings.py:
import os
GDAL_LIBRARY_PATH = r'C:\OSGeo4W\bin\gdal300'
  1. Now, run the server still if it doesn’t work. Run the following commands in terminal.
set OSGEO4W_ROOT=C:\OSGeo4W
set PYTHON_ROOT=C:\Python3X 
set GDAL_DATA=%OSGEO4W_ROOT%\share\gdal 
set PROJ_LIB=%OSGEO4W_ROOT%\share\proj

1👍

I had the same error “The specified module could not be found,” even though gdal204.dll was present at the expected location, with the right architecture (which I verified by adding asserts in the Python code and loading the DLL from a C program).

It turned out to be an issue with the Python 3.7 app from the Microsoft Store, the one that automatically installs the first time you type python on the command line.
This version of Python refuses to load the GDAL DLL; I don’t know if it’s a bug or a security feature.

I fixed the issue by:

  • uninstalling the Python app
  • removing the app execution aliases (as explained in the app description)
  • installing the regular Python package from python.org

1👍

  1. Download GDAL wheel file that is supported for your platform from here.
  2. Open the command window where the downloaded file is located and activate your virtual environment.

Activating your virtual environment

  1. Then install the wheel using command pip install name_of_the_file .

Screenshot 2

You will see osgeo folder has been created in the location ‘…\Envs\my_django\Lib\site-packages\’ .

  1. Go to osgeo folder and copy the entire path of your gdalxxx.dll file and add to setting.py file as gdal library path. For example

    GDAL_LIBRARY_PATH = r'C:\Users\WIN8\Envs\my_django\Lib\site-packages\osgeo\gdal300.dll'

1👍

Creating the map compatible database

I recently got a new laptop and had to install Python and the related software on my new machine. I was trying to create a geodjango compatible database using OSGeo4W and had forgotten about the role of PostgreSQL and pgAdmin4.

When you install the latest PostgreSQL setup from https://www.enterprisedb.com/downloads/postgres-postgresql-downloads, you get pgAdmin4 at the same time.

Note – Ecommerce is my personal server group.

enter image description here

When you create a PostgreSQL 13 server, your server group will be different to mine.
enter image description here

When I first opened pgAdmin4 on my new machine, I kept the default username ‘postgres’. If when you first logged into pgAdmin4 you chose your own username, you will have to change the values below accordingly.
enter image description here

Connecting to the database

In my code I had

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']
GDAL_LIBRARY_PATH = r'C:\OSGeo4W64\bin\gdal300.dll'

I was getting an error message regarding the GDAL_LIBRARY_PATH so checked Windows Explorer. On discovering I did not have a gdal300.dll file, I changed my code to

GDAL_LIBRARY_PATH = r'C:\OSGeo4W64\bin\gdal301.dll'

enter image description here

0👍

Just to follow up on the nice and detailed answer of Udi (cannot comment directly as my rep is under 50, it is the answer marked as the most useful);

After many hours I tried his offered solution which also did not work for me.
I was getting the following error:

OSError: [WinError 193] %1 is not a valid Win32 application

But I stayed there and found out that although I’m running 64 python and operating system (for sure), it kept looking for 32 bit (OSGeo4W) folder. What eventually let me pass was to copy contents of the OSGeo4W64 folder to the OSGeo4W. Hope it will save you time.

One more note:

Be sure that you edit libgdal.py file in your environment folder. It may exist in more than one place – your python folder and environment folder – if you edit the libgdal in your python dir, it is not going to work.

0👍

pip install GDAL

(From https://www.lfd.uci.edu/~gohlke/pythonlibs/)

Add In %PYTHON_ROOT%\Lib\site-packages\django\contrib\gis\gdal\libgdal.py

import osgeo

Check Gdal Vesion In: %PYTHON_ROOT%\Lib\site-packages\osgeo\gdal303.dll

lib_names = ['gdal303', 'gdal202', 'gdal201', 'gdal20', 'gdal111', 'gdal110', 'gdal19']

0👍

Here is a solution to this gdal problem in Django: https://www.dark-hamster.com/application/how-to-solve-error-message-could-not-find-the-gdal-library-when-running-django-application-in-microsoft-windows/?utm_source=pocket_saves.

  1. First of all ensure that you have downloaded the OSGeo4W package from here. Install it to your computer. It will take quite some time since the full package is >1GB.

  2. Ensure that you have a folder C:\OSGeo4W in your C Drive.

  3. Go to C:\OSGeo4W\bin and check for a file such as gdal306.dll. In my case I have gdal306.dll and gdal307.dll. I went with the former.

  4. Go to your settings.py file in your Django project.

  5. If you pasted your Django error somewhere, it read something like this:

django.core.exceptions.ImproperlyConfigured: Could not find the GDAL library (tried "gdal202", "gdal201", "gdal20", "gdal111", "gdal110", "gdal19"). Is GDAL installed? If it is, try setting GDAL_LIBRARY_PATH in your settings.

We will give it a taste of its own medicine by creating a variable GDAL_LIBRARY_PATH in your settings.py file.

  1. Copy the path to your gdal306.dll file from OSGeo4W folder. In my case it was, C:\OSGeo4W\bin\gdal306.dll. Paste it as the path to the GDAL_LIBRARY_PATH like so:

GDAL_LIBRARY_PATH = r'C:\OSGeo4W\bin\gdal306.dll'

Note – make sure to add r’absolute_path’ instead of ‘absolute_path’ otherwise this error will come – FileNotFoundError: Could not find module ‘C:\OSGeo4in\gdal307.dll’ (or one of its dependencies). Try using the full path with constructor syntax.

  1. Run your django project once more, if all is ok, there shouldn’t be that error again.

Leave a comment