GeoDjango on Windows: try setting GDAL_LIBRARY_PATH in your settings

I have already done this a dozen times, but this time something is not working.

Following the docs:

https://docs.djangoproject.com/en/1.11/ref/contrib/gis/install/#windows

I am trying to configure GeoDjango on a Windows machine (this virtual Windows 10 is configured on paperpace.com). There seems to be a problem with the PATH settings, but I can't figure out what it is. I followed the instructions outlined in the instructions. I checked the PATH variables and everything looks fine. I tried pointing them to the 32-bit and 64-bit versions of OSGeo4Win. Despite this, each time I get the following output:

C:\Python\lib\site-packages\floppyforms\__init__.py:21: UserWarning: Unable to import floppyforms.gis, geometry widgets not available "Unable to import floppyforms.gis, geometry widgets not available") Traceback (most recent call last): File "manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "C:\Python\lib\site-packages\django\core\management\__init__.py", line 363, in execute_from_command_line utility.execute() File "C:\Python\lib\site-packages\django\core\management\__init__.py", line 337, in execute django.setup() File "C:\Python\lib\site-packages\django\__init__.py", line 27, in setup apps.populate(settings.INSTALLED_APPS) File "C:\Python\lib\site-packages\django\apps\registry.py", line 108, in populate app_config.import_models() File "C:\Python\lib\site-packages\django\apps\config.py", line 202, in import_models self.models_module = import_module(models_module_name) File "C:\Python\lib\importlib\__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 978, in _gcd_import File "<frozen importlib._bootstrap>", line 961, in _find_and_load File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 655, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 678, in exec_module File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed File "C:\Python\lib\site-packages\django\contrib\auth\models.py", line 4, in <module> from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager File "C:\Python\lib\site-packages\django\contrib\auth\base_user.py", line 52, in <module> class AbstractBaseUser(models.Model): File "C:\Python\lib\site-packages\django\db\models\base.py", line 124, in __new__ new_class.add_to_class('_meta', Options(meta, app_label)) File "C:\Python\lib\site-packages\django\db\models\base.py", line 330, in add_to_class value.contribute_to_class(cls, name) File "C:\Python\lib\site-packages\django\db\models\options.py", line 214, in contribute_to_class self.db_table = truncate_name(self.db_table, connection.ops.max_name_length()) File "C:\Python\lib\site-packages\django\db\__init__.py", line 33, in __getattr__ return getattr(connections[DEFAULT_DB_ALIAS], item) File "C:\Python\lib\site-packages\django\db\utils.py", line 211, in __getitem__ backend = load_backend(db['ENGINE']) File "C:\Python\lib\site-packages\django\db\utils.py", line 115, in load_backend return import_module('%s.base' % backend_name) File "C:\Python\lib\importlib\__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "C:\Python\lib\site-packages\django\contrib\gis\db\backends\postgis\base.py", line 5, in <module> from .features import DatabaseFeatures File "C:\Python\lib\site-packages\django\contrib\gis\db\backends\postgis\features.py", line 1, in <module> from django.contrib.gis.db.backends.base.features import BaseSpatialFeatures File "C:\Python\lib\site-packages\django\contrib\gis\db\backends\base\features.py", line 4, in <module> from django.contrib.gis.db.models import aggregates File "C:\Python\lib\site-packages\django\contrib\gis\db\models\__init__.py", line 3, in <module> from django.contrib.gis.db.models.aggregates import * # NOQA File "C:\Python\lib\site-packages\django\contrib\gis\db\models\aggregates.py", line 1, in <module> from django.contrib.gis.db.models.fields import ExtentField File "C:\Python\lib\site-packages\django\contrib\gis\db\models\fields.py", line 3, in <module> from django.contrib.gis import forms, gdal File "C:\Python\lib\site-packages\django\contrib\gis\forms\__init__.py", line 3, in <module> from .fields import ( # NOQA File "C:\Python\lib\site-packages\django\contrib\gis\forms\fields.py", line 4, in <module> from django.contrib.gis.geos import GEOSException, GEOSGeometry File "C:\Python\lib\site-packages\django\contrib\gis\geos\__init__.py", line 5, in <module> from .collections import ( # NOQA File "C:\Python\lib\site-packages\django\contrib\gis\geos\collections.py", line 11, in <module> from django.contrib.gis.geos.geometry import GEOSGeometry, LinearGeometryMixin File "C:\Python\lib\site-packages\django\contrib\gis\geos\geometry.py", line 11, in <module> from django.contrib.gis import gdal File "C:\Python\lib\site-packages\django\contrib\gis\gdal\__init__.py", line 28, in <module> from django.contrib.gis.gdal.datasource import DataSource File "C:\Python\lib\site-packages\django\contrib\gis\gdal\datasource.py", line 39, in <module> from django.contrib.gis.gdal.driver import Driver File "C:\Python\lib\site-packages\django\contrib\gis\gdal\driver.py", line 5, in <module> from django.contrib.gis.gdal.prototypes import ds as vcapi, raster as rcapi File "C:\Python\lib\site-packages\django\contrib\gis\gdal\prototypes\ds.py", line 9, in <module> from django.contrib.gis.gdal.libgdal import GDAL_VERSION, lgdal File "C:\Python\lib\site-packages\django\contrib\gis\gdal\libgdal.py", line 44, in <module> 'GDAL_LIBRARY_PATH in your settings.' % '", "'.join(lib_names) django.contrib.gis.gdal.error.GDALException: Could not find the GDAL library (tried "gdal111", "gdal110", "gdal19", "gdal18", "gdal17"). Try setting GDAL_LIBRARY_PATH in your settings. 

Does anyone have any ideas?

UPDATE: I went to the store and bought a new laptop, and I am still facing the same issue. Maybe there is a problem with the version with some GeoDjango dependencies? I am at a loss what to do, but for me this is an emergency.

+6
source share
7 answers

As a result, the problem was version mismatch between Django and GDAL . Django did not search for the correct file name ( gdal202.dll in my case).

To fix it, I needed to add str('gdal202') to the following file on line 26:

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

If this problem OSGeo4W\bin , you can look at the OSGeo4W\bin to find out which gdalxxx.dll it should look for in Django.

+7
source

Adding

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

for django settings working for me

+3
source

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

This is due to the presence of several sqlite3.dll in your system. If the path is one of the first, a conflict arises for gdal202.

You can expand the answers of Adam Starr by also adding a change to the working directory.

 #Set working directory to actual working directory of gdal #This is required to prevent any conflicts with older SQLITE versions, #fe 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) 
+2
source

If you use the Django tutorial and install GDAL through OSGeo4W, when changing your Windows environment, make sure that you determine the correct versions of Python and OSGeo4W.

For example, when installing 64-bit OSGeo4W, you must specify the path to the environment:

 set OSGEO4W_ROOT=C:\OSGeo4W64 

and for Python 3.5.2:

 set PYTHON_ROOT=C:\Python36 

Make sure you do not confuse Python installations in virtualenv with the Python system.

Install Django only after you have changed the environment correctly.

+2
source

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

+1
source

Decision:

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

    b) Also add this code:

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

0
source

When making changes, a problem similar to this occurs, enter a description of the image here.

Maybe the dll library (gdal202.dll) is not working. so we can replace the entire GDAL library with a GDAL-whl package from https://download.lfd.uci.edu/pythonlibs/r5uhg2lo/GDAL-2.3.2-cp36-cp36m-win_amd64.whl download this package whl, rename it to GDAL-2.3.2-cp36-cp36m-win_amd64.whl.zip enter the description of the image here, extract the osgeo folder in one place (D: \ ProgramData \ osgeo) and change the GDAL_DATA PROJ_LIB and specify the path to it in a subfolder

which means changing the 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%" 

in

 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: do not forget to rename "D: \ ProgramData \ osgeo \ gdal203.dll" to "D: \ ProgramData \ osgeo \ gdal202.dll"

0
source

Source: https://habr.com/ru/post/1268138/


All Articles