1

I'm following this tutorial: https://docs.djangoproject.com/en/2.2/ref/contrib/gis/tutorial/, I installed every component necessary. But it seems like Django can't find the gdal module.

I have gdal installed by pip using the wheels in https://www.lfd.uci.edu/~gohlke/pythonlibs/#gdal. It is located in the same directory than python37: C:\Python37\Lib\site-packages\osgeo

My python version is Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32 and I installed django using its pip module.

I tried the solutions described in this SO questions: https://stackoverflow.com/questions/49139044/geodjango-on-windows-could-not-find-the-gdal-library-oserror-winerror-12 https://stackoverflow.com/questions/23197431/geodjango-cant-find-gdal-while-python-can

And including the gdal path GDAL_LIBRARY_PATH = 'C:\\Python37\\Lib\\site-packages\\osgeo\\gdal300.dll'

With no results.

This is the error traceback:

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:\Python37\lib\threading.py", line 926, in _bootstrap_inner
    self.run()
  File "C:\Python37\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Python37\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "C:\Python37\lib\site-packages\django\core\management\commands\runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "C:\Python37\lib\site-packages\django\utils\autoreload.py", line 77, in raise_last_exception
    raise _exception[1]
  File "C:\Python37\lib\site-packages\django\core\management\__init__.py", line 337, in execute
    autoreload.check_errors(django.setup)()
  File "C:\Python37\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "C:\Python37\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Python37\lib\site-packages\django\apps\registry.py", line 114, in populate
    app_config.import_models()
  File "C:\Python37\lib\site-packages\django\apps\config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Python37\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Python37\lib\site-packages\django\contrib\auth\models.py", line 2, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "C:\Python37\lib\site-packages\django\contrib\auth\base_user.py", line 47, in <module>
    class AbstractBaseUser(models.Model):
  File "C:\Python37\lib\site-packages\django\db\models\base.py", line 117, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "C:\Python37\lib\site-packages\django\db\models\base.py", line 321, in add_to_class
    value.contribute_to_class(cls, name)
  File "C:\Python37\lib\site-packages\django\db\models\options.py", line 204, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "C:\Python37\lib\site-packages\django\db\__init__.py", line 28, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "C:\Python37\lib\site-packages\django\db\utils.py", line 201, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "C:\Python37\lib\site-packages\django\db\utils.py", line 110, in load_backend
    return import_module('%s.base' % backend_name)
  File "C:\Python37\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "C:\Python37\lib\site-packages\django\contrib\gis\db\backends\postgis\base.py", line 6, in <module>
    from .features import DatabaseFeatures
  File "C:\Python37\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:\Python37\lib\site-packages\django\contrib\gis\db\backends\base\features.py", line 3, in <module>
    from django.contrib.gis.db.models import aggregates
  File "C:\Python37\lib\site-packages\django\contrib\gis\db\models\__init__.py", line 3, in <module>
    import django.contrib.gis.db.models.functions  # NOQA
  File "C:\Python37\lib\site-packages\django\contrib\gis\db\models\functions.py", line 4, in <module>
    from django.contrib.gis.db.models.fields import BaseSpatialField, GeometryField
  File "C:\Python37\lib\site-packages\django\contrib\gis\db\models\fields.py", line 3, in <module>
    from django.contrib.gis import forms, gdal
  File "C:\Python37\lib\site-packages\django\contrib\gis\forms\__init__.py", line 3, in <module>
    from .fields import (  # NOQA
  File "C:\Python37\lib\site-packages\django\contrib\gis\forms\fields.py", line 2, in <module>
    from django.contrib.gis.gdal import GDALException
  File "C:\Python37\lib\site-packages\django\contrib\gis\gdal\__init__.py", line 28, in <module>
    from django.contrib.gis.gdal.datasource import DataSource
  File "C:\Python37\lib\site-packages\django\contrib\gis\gdal\datasource.py", line 39, in <module>
    from django.contrib.gis.gdal.driver import Driver
  File "C:\Python37\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:\Python37\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:\Python37\lib\site-packages\django\contrib\gis\gdal\libgdal.py", line 47, in <module>
    lgdal = CDLL(lib_path)
  File "C:\Python37\lib\ctypes\__init__.py", line 364, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] No se puede encontrar el módulo especificado

Could anybody help me?

0

I resolved this by reinstalling a previous gdal version supported by django (gdal204 instead of gdal300), removing the system enviroment variables refered to gdal and including these in the django project (settings.py file):

os.environ['GDAL_DATA'] = r"C:\Python37\Lib\site-packages\osgeo\data\gdal"
os.environ['PROJ_LIB'] = r"C:\Python37\Lib\site-packages\osgeo\data\proj"
os.environ['PATH'] = r"C:\Python37\Lib\site-packages\osgeo" + os.environ['PATH']
GDAL_LIBRARY_PATH = r'C:\Python37\Lib\site-packages\osgeo\gdal204.dll' 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.