[Fixed]-GeoDjango on Windows: Try setting GDAL_LIBRARY_PATH in your settings

23👍

The issue ended up being a version mismatch between Django and GDAL. Django was not searching for the correct file name (gdal202.dll in my case).

Fixing it required me to add str('gdal202') to the following file on line 26:

(Python Root)\Lib\site-packages\django\contrib\gis\gdal\libgdal.py

Note that if you are working in a Virtual Environment, then Python Root will actually be wherever that is ie: Users\YourName\Envs\project

If this issue reoccurs, you can look through your C:\OSGeo4W\bin directory to figure out which gdalxxx.dll it is that Django needs to search for.

In addition, make sure that you are using the 32-bit versions of Python and OSGeo4Win. Otherwise, you may see the same failure.

18👍

Adding

GDAL_LIBRARY_PATH = r'C:\OSGeo4W64\bin\gdal202'

to the django settings worked for me

4👍

In addition to the previous comments. You might have a [WinError 127] The specified procedure could not be found.

This appears due to having multiple sqlite3.dll on your system. If the path hits those first a conflict appears for gdal202.

You can extend adam starrh answers by adding also a change of working directory.

#Set working directory to actual working directoy of gdal
#This is required to prevent any conflicts with older SQLITE versions, 
#f.e. in python path  
#Prevents error in GDAL 202
#BASE_WORKINGDIRECTORY to change it back later if you need to
BASE_WORKDIRECTORY = os.getcwd()
os.chdir(os.path.dirname(lib_path))

# This loads the GDAL/OGR C library
lgdal = CDLL(lib_path)

3👍

I got same error.

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

First, insall OSGeo4W
, and then add following code 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']

and run python manage.py check
if you got the error still, please go to C:\OSGeo4W64 or C:\OSGeo4W.
There you can find gdalxxx.dll. please rename the file name to ‘gdal202’ or ‘gdal203’ in error message.
Please run python manage.py check, That must work.

3👍

I had the same problem in Django in a virtual environment. What solved my problem was:

  1. In settings.py add the path to the gdalxxx.dll your app is trying to use, for me it was

     GDAL_LIBRARY_PATH = r'C:\Users\<User>\Documents\<proj>\Lib\site-packages\osgeo\gdal301'
    

This alone could possibly solve your problem, but Django may still complain about missing GEOS library, in that case:

  1. In settings.py add the path to the geos_c.dll your app is trying to use, for me it was:

    GEOS_LIBRARY_PATH = r'C:\Users\<User>\Documents\<proj>\Lib\site-packages\shapely\DLLs\geos_c'
    

2👍

If you’re using the Django tutorial and installing GDAL via OSGeo4W, when modifying the Windows environment, ensure you’ve defined the proper Python and OSGeo4W versions.

E.g., when installing 64-bit OSGeo4W, the environment path should be set:

set OSGEO4W_ROOT=C:\OSGeo4W64

and for Python 3.5.2:

set PYTHON_ROOT=C:\Python36

Be sure you’re not confusing Python installations in a virtualenv with the system Python.

Only install Django after you’ve properly modified the environment.

👤tmpbtz

1👍

For me this issue was solved on Windows simply by adding the path to the osgeo python package to the Environment variables (C:\Python36\Lib\site-packages\osgeo). It seems that after doing this, django managed to find the gdal202.dll file.

👤Hakim

0👍

Solution:

  1. Install GDAL from pip: pip install GDAL, if does not work install
    it from wheel: https://www.lfd.uci.edu/~gohlke/pythonlibs/#gdal
  2. a) If you use virtual environment just add this code to settings.py:
    os.environ["PATH"] += os.pathsep + BASE_DIR + '\\venv\\Lib\\site-packages\\osgeo'

    b) Else add this code:

    from distutils.sysconfig import get_python_lib
    os.environ["PATH"] += os.pathsep + get_python_lib() + '\\osgeo'

0👍

When makemigretions ,it appare an issue like this
enter image description here

Maybe the dll(gdal202.dll) lib doesn’t work.
so we can replace the whole GDAL lib with GDAL-whl-package from https://download.lfd.uci.edu/pythonlibs/r5uhg2lo/GDAL-2.3.2-cp36-cp36m-win_amd64.whl
download this whl package, rename it to GDAL-2.3.2-cp36-cp36m-win_amd64.whl.zip
enter image description here
extract the osgeo folder to one place (D:\ProgramData\osgeo) and modify the GDAL_DATA PROJ_LIB and path to it’s subfolder

which means change Windows environment
from

set OSGEO4W_ROOT=C:\OSGeo4W
set PYTHON_ROOT=C:\Python3X
set GDAL_DATA=%OSGEO4W_ROOT%\share\gdal
set PROJ_LIB=%OSGEO4W_ROOT%\share\proj
set PATH=%PATH%;%PYTHON_ROOT%;%OSGEO4W_ROOT%\bin
reg ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_EXPAND_SZ /f /d "%PATH%"
reg ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v GDAL_DATA /t REG_EXPAND_SZ /f /d "%GDAL_DATA%"
reg ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v PROJ_LIB /t REG_EXPAND_SZ /f /d "%PROJ_LIB%"

to

set OSGEO4W_ROOT=D:\ProgramData\osgeo
set PYTHON_ROOT=C:\Python3X
set GDAL_DATA=%OSGEO4W_ROOT%\data\gdal
set PROJ_LIB=%OSGEO4W_ROOT%\data\proj
set PATH=%PATH%;%PYTHON_ROOT%;%OSGEO4W_ROOT%\bin
reg ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_EXPAND_SZ /f /d "%PATH%"
reg ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v GDAL_DATA /t REG_EXPAND_SZ /f /d "%GDAL_DATA%"
reg ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v PROJ_LIB /t REG_EXPAND_SZ /f /d "%PROJ_LIB%"

PS : Don’t forget to rename “D:\ProgramData\osgeo\gdal203.dll” to “D:\ProgramData\osgeo\gdal202.dll”

0👍

issue has been fixed by applying the fixes in this link

Leave a comment