Solving GeoDjango Errors – Could not find the GDAL library and OSError: [WinError 126] The specified module could not be found”

GeoDjango Errors – Could not find the GDAL library and OSError: [WinError 126] The specified module could not be found” is an error which occurs when GDAL is not installed or not installed correctly.

This post is a guide showing you why you are having this error and how you can get rid of it in the most efficient way possible, I will also include some alternative solutions that may help you.

Explaining the GeoDjango Errors – Could not find the GDAL library and OSError: [WinError 126] The specified module could not be found”

This is an error which occurs when GDAL is not installed or not installed correctly.

The error message should look like the error in the example bellow, make sure you have the same error message in order to avoid confusion.

                                                                       #
OSError: [WinError 126] The specified module could not be found
                                                                       #

Bellow we will describe how the error can be solved. With multiple possible solutions.

Solution 1 : Download and install the GDAL wheel and MORE.

The easiest solution to the issue is to Download and install the GDAL wheel.

You can do that by opening this link and finding the file to download.

https://www.lfd.uci.edu/~gohlke/pythonlibs/#gdal

Once you have downloaded the file, run the command bellow to install it.

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

Navigate to this file.

                                                                       #
/path/to/virtual_env/Lib/site-packages/django/contrib/gis/gdal/libgdal.py
                                                                       #

Add ‘gdal300’ to line 23 of the file above. observe the bellow example

                                                                       #
lib_names = ['gdal300', 'gdal204', 'gdal203', 'gdal202', 'gdal201', 'gdal20']
                                                                       #

Add the following code to the settings.py file located in your project folder.

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

Try this detailed solution and your error should be gone. If it fails please try the solution bellow.

Solution 2 : install OSGeo4W 

The second and last solution is to install

Head to the following link https://trac.osgeo.org/osgeo4w/

Choose the Express Web-GIS Install and continue, uncheck Apache and MapServer and select GDAL.

Do get the version of OSGeo4W corresponding to your python version (64 bit or 32 bit).

You should also install to one of the directories bellow, depending on your version

                                                                       #
C:\OSGeo4W or C:\OSGeo4W64
                                                                       #

Add the snippets bellow in order to settings.py

Start by importing and initializing OSGEO4W

                                                                       #
    import os
if os.name == 'nt':
    import platform
    OSGEO4W = r"C:\OSGeo4W"
    if '64' in platform.architecture()[0]:
    OSGEO4W += "64"
                                                                       #

And finally setting the environment os.enviro

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

I hope the fix above fixed your problem, good luck with the scripts to come.

If the solutions above helped you, consider supporting us on Kofi, any help is appreciated.

Summing-up

I hope this article has been helpful and helped you solve this error. I hope you continue coding and learning Python, errors are part of the fun even when we hate them, cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/