GeoDjango on Windows: Try setting GDAL_LIBRARY_PATH in your settings

此生再无相见时 提交于 2019-11-30 07:38:12

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

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

Adding

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

to the django settings worked for me

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)

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.

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.

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'

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"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!