16

I've done this a dozen times before, but something isn't working this time..

Following the docs:

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

I'm trying to set up GeoDjango on a Windows machine (this one is a virtual windows 10 set up on paperspace.com). There seems to be a problem with my PATH settings, but I can't figure out what it is. I've run the commands highlighted in the instructions. I've checked my PATH variables and everything seems ok. I've tried pointing them to both the 32-bit and 64-bit versions of OSGeo4Win. Regardless, I get the following output every time:

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 running into the same issue. Could it be an versioning issue with some of the GeoDjango dependencies? I'm at a loss for what to do, but it's kind of an emergency for me.

Adam Starrh
  • 6,428
  • 8
  • 50
  • 89

10 Answers10

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.

Adam Starrh
  • 6,428
  • 8
  • 50
  • 89
18

Adding

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

to the django settings worked for me

  • what django settings, like what file? – ziggy Feb 28 '18 at 16:40
  • in my project it is located at project_folder/config/settings/base.py. More about django settings https://docs.djangoproject.com/en/2.0/ref/settings/ https://docs.djangoproject.com/en/2.0/topics/settings/ – Saltanat Alikhanova Mar 14 '18 at 09:51
  • 2
    Keep in mind that this approach will be disruptive if you switch between Windows and Mac or Linux developing environments. – Adam Starrh Apr 11 '18 at 21:12
  • Like @AdamStarrh mentions, this is disruptive if your application is shared within multiple OSs (developers for example). For MacOS specifically, the cause of this is likely because GDAL has been installed through homebrew, which installs libraries to its own "opt" folder. Symbolic linking this to `/usr/local/lib` fixes this issue. – Larcho Feb 27 '23 at 22:02
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)
Mike - kentivo
  • 151
  • 1
  • 3
  • this is in `Lib\site-packages\django\contrib\gis\gdal\libgdal.py` – danleyb2 Jun 03 '18 at 04:05
  • I'm getting [WinError 127] error too, but the suggested approach does not solve the problem, unfortunately. When calling makemigrations from PyCharm, I get the error "The procedure entry point sqlite3_column_origin_name could not be located in the dynamic link library C:\OSGeo4W64\bin\gdal202.DLL". Haven't been able to find any solution for this on the net – silentsurfer Nov 28 '18 at 09:46
  • Update: I had to do this too after installing GDAL via pip from: https://www.lfd.uci.edu/~gohlke/pythonlibs/#gdal You're a life saver!! – silentsurfer Nov 29 '18 at 19:01
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'
    
Ivan Kljun
  • 75
  • 5
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
  • 21
  • 1
  • Can you please elaborate on what you mean by "be sure you're not confusing Python installations in a virtualenv with the system Python"? – silentsurfer Nov 27 '18 at 12:44
  • 1
    ensure the path environment assignment is reflective of the Python version you'll be using in the active virtual env – tmpbtz Nov 28 '18 at 21:43
2

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.

DreH-World
  • 21
  • 4
  • am I glad I scrolled to the end... But I don't really understand why this works and the above does not. Still, very grateful. Thx – bluppfisk Nov 04 '20 at 19:46
  • Turns out it's down to adding `c:\osgeo4w64\bin` to the windows path. But strange that setting `GDAL_LIBRARY_PATH` to the very file doesn't work – bluppfisk Nov 04 '20 at 19:52
  • It works because you are giving the GDAL file a name that matches the specific filename queries that Django is looking for. – Adam Starrh Nov 25 '20 at 18:49
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
  • 3,225
  • 5
  • 37
  • 75
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

Husam Alhwadi
  • 383
  • 3
  • 11
  • Keep in mind that this approach will be disruptive if you switch between Windows and Mac or Linux developing environments. – Adam Starrh Sep 21 '21 at 20:01
  • Yes, thats why Pathlib and similar functions can help to remove such concerns especially when go to production deployment , What I have provided doesn't fix the issue completely, I have faced another issue when updating the multipolygon filed and it solved by applying updated fixes (see the updated post) – Husam Alhwadi Sep 22 '21 at 13:18