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 as Python 3.7: 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 questions:

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
  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
  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
  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
  File "C:\Python37\lib\site-packages\django\apps\registry.py", line 114, in populate
  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?

2 Answers 2


I resolved this by reinstalling a previous gdal version supported by django (gdal204 instead of gdal300), from here, version 2.4.1, 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' 
  • 1
    I hadn't ever installed gdal into my virtual environment before, but because I need a different version of it globally for other applications, this worked perfectly! thanks for posting this answer.
    – mr.adam
    Mar 11, 2020 at 17:37
  • 1
    After testing some, I realized your PATH line is a little wrong, it needs a ; in it like so: os.environ['PATH'] = r"C:\Python37\Lib\site-packages\osgeo" + ';' + os.environ['PATH'].
    – mr.adam
    Mar 11, 2020 at 21:49
  • Thanks! Finally worked for me Jun 5, 2020 at 13:02

Before installing Django,

  1. Setup GDAL

    run 'OSGeo4W installer'

    in CMD (run as administrator), execute code by code:

    set OSGEO4W_ROOT=C:\OSGeo4W
    set PYTHON_ROOT=C:\Python310
    set GDAL_DATA=%OSGEO4W_ROOT%\share\gdal
    set PROJ_LIB=%OSGEO4W_ROOT%\share\proj
    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%"
  2. After installing Django App

    Added GeoDjango in 'settings.py' file as follows

      # [...]
    GDAL_LIBRARY_PATH = "C:\\OSGeo4W\\bin\\gdal306.dll"

Now it works properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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