GeoDjango on Windows: "Could not find the GDAL library" / "OSError: [WinError 126] The specified module could not be found"

Yorian picture Yorian · Mar 6, 2018 · Viewed 18.3k times · Source

I've been trying to setup my windows computer such that I can have a local postgreSQL with PostGIS extension. With this installed I hope to be able to create a project with geodjango locally before putting it in the cloud. I have been working with Django for a little while now on my local machine with the SQLite DB, but since the next project will partly be based on coordinate based data I wanted to setup the right environment.

Import note: I've installed mini-conda to run in a seperate environment. I do activate this environment "development" when I work though

I've tried to follow most of the geodjango information/tutorials online, but can't get it to work. What I've done (mostly followed this: https://docs.djangoproject.com/en/2.0/ref/contrib/gis/install/#windows):

  1. Download and install the latest (10.3) PostgreSQL setup from https://www.enterprisedb.com/downloads/postgres-postgresql-downloads
  2. After installation I also installed used the Application Stack Builder to install PostGis
  3. I've installed OSGeo4W from https://trac.osgeo.org/osgeo4w/
  4. I've created a batch script as described on the geodjango website (https://docs.djangoproject.com/en/2.0/ref/contrib/gis/install/#windows) and ran it as administrator (except for the part where it sets the path to python, cause python was already in there since I've been using python for a while now)
  5. I've tried some command in psql shell and I think I've created a database with name: geodjango, username: **** and pass: ****.
  6. I don't know if I have given the geodjango user all priveleges, but I suspect so.

After all of this I created a new django project and in settings.py I've added some parts:

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.gis',
'nameOfMyApp',
]

I've also got this in settings.py:

DATABASES = {
'default': {
    'ENGINE': 'django.contrib.gis.db.backends.postgis',
    'NAME': 'geodjango',
    'USER': '****',
    'PASSWORD': '****',
    'HOST': 'localhost',
}
}

# FOR GEODJANGO
POSTGIS_VERSION = (2, 4, 3)

When I try to set up the database in django I run (in the right folder):

python manage.py makemigrations

I get the following error:

django.core.exceptions.ImproperlyConfigured: Could not find the GDAL library (tried "gdal202", "gdal201", "gdal20", "gdal111", "gdal110", "gdal19"). Is GDAL installed? If it is, try setting GDAL_LIBRARY_PATH in your settings.

I've tried to fix that, but nothing seems to work. Can anybody give me some help in setting this all up locally?

Update 7-3-2018:

Now I get the following error:

OSError: [WinError 126] The specified module could not be found

(while the .dll is there...)

Answer

Udi picture Udi · Mar 7, 2018

I have found the following to work for windows:

  • Run python to check if your python is 32 or 64 bit.
  • Install corresponding OSGeo4W (32 or 64 bit) into C:\OSGeo4W or C:\OSGeo4W64:
    • Note: Select Express Web-GIS Install and click next.
    • In the ‘Select Packages’ list, ensure that GDAL is selected; MapServer and Apache are also enabled by default, may be unchecked safely.
  • Make sure the following is included 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']
    
  • Run python manage.py check to verify geodjango is working correctly.