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

Question:

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…)

Asked By: Yorian

||

Answers:

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"sharegdal"
        os.environ['PROJ_LIB'] = OSGEO4W + r"shareproj"
        os.environ['PATH'] = OSGEO4W + r"bin;" + os.environ['PATH']
    
  • Run python manage.py check to verify geodjango is working correctly.

Answered By: Udi

In my case (Windows10Pro+Python3.7.1), having the (automatically chosen) dll present was not enough, namely gdal111.dll.

I realized that I also had gdal204.dll located at C:OSGeo4Wbin and tried to “enrich” the list variable named lib_names with 'gdal204', at line 24 (regarding Windows NT shared libraries) of %PYTHON_ROOT%Libsite-packagesdjangocontribgisgdallibgdal.py, i.e.

#[...]
elif os.name == 'nt':
    # Windows NT shared libraries
    lib_names = ['gdal204', 'gdal202', 'gdal201', 'gdal20', 'gdal111', 'gdal110', 'gdal19']
#[...]            ^^^^^^^

No negative consequences for now.

Answered By: keepAlive

For Microsoft Windows 10 & Python3.6.8, I installed GDAL 2.3.3 from Unofficial Windows Binaries for Python Extension Packages, modified libgdal.py adding gdal203 in the lib_names list envLibsite-packagesdjangocontribgisgdallibgdal.py.

Finally, added osgeo and proj to the PATH, and set the GDAL_LIBRARY_PATH as below (beginning of settings.py):

os.environ['PATH'] = os.path.join(BASE_DIR, r'envLibsite-packagesosgeo') + ';' + os.environ['PATH']

os.environ['PROJ_LIB'] = os.path.join(BASE_DIR, r'env3Libsite-packagesosgeodataproj') + ';' + os.environ['PATH']

GDAL_LIBRARY_PATH = os.path.join(BASE_DIR, r'envLibsite-packagesosgeogdal203.dll') 

In this case, env is my Python environment.

Answered By: user42121

Steps to follow:

  1. Run python to check if your python is 32 or 64 bit.
  2. Install corresponding OSGeo4W (32 or 64 bit) into C:OSGeo4W or C:OSGeo4W64:
    Note: Select Express Web-GIS Install and click next.
  3. In the ‘Select Packages’ list, ensure that GDAL is selected; MapServer and Apache are also enabled by default.
  4. Make sure the following is included in your settings.py:
import os
GDAL_LIBRARY_PATH = r'C:OSGeo4Wbingdal300'
  1. Now, run the server still if it doesn’t work. Run the following commands in terminal.
set OSGEO4W_ROOT=C:OSGeo4W
set PYTHON_ROOT=C:Python3X 
set GDAL_DATA=%OSGEO4W_ROOT%sharegdal 
set PROJ_LIB=%OSGEO4W_ROOT%shareproj
Answered By: karthik k

After updating some OSGEO4W on my Windows 10 Pro machine I started having problems with the GDAL bindings again. I previously used a combination of the solutions posted here and with this tutorial.

This is what works for me using Windows 10 Pro 64-bit, Django 3.0.6 and GDAL 3.0.4 using a python 3.7 virtual environment. I have tested it without OSGEO4W and it seems to work.

First, download the GDAL wheel from Christoph Gohlke’s Unofficial Windows Binaries for Python Extension Packages.

pip install "/path/to/GDAL‑3.0.4‑cp37‑cp37m‑win_amd64.whl"

Modify the libgdal.py file in the virtual envrironment site packages by adding ‘gdal300’ to line 23 of the Django GDAL package python file (/path/to/virtual_env/Lib/site-packages/django/contrib/gis/gdal/libgdal.py):

elif os.name == 'nt':
    # Windows NT shared libraries
    lib_names = ['gdal300', 'gdal204', 'gdal203', 'gdal202', 'gdal201', 'gdal20']

Finally, in your settings.py file in your Django project add

if os.name == 'nt':
    VENV_BASE = os.environ['VIRTUAL_ENV']
    os.environ['PATH'] = os.path.join(VENV_BASE, 'Lib\site-packages\osgeo') + ';' + os.environ['PATH']
    os.environ['PROJ_LIB'] = os.path.join(VENV_BASE, 'Lib\site-packages\osgeo\data\proj') + ';' + os.environ['PATH']
Answered By: kingurr

I had the same error “The specified module could not be found,” even though gdal204.dll was present at the expected location, with the right architecture (which I verified by adding asserts in the Python code and loading the DLL from a C program).

It turned out to be an issue with the Python 3.7 app from the Microsoft Store, the one that automatically installs the first time you type python on the command line.
This version of Python refuses to load the GDAL DLL; I don’t know if it’s a bug or a security feature.

I fixed the issue by:

  • uninstalling the Python app
  • removing the app execution aliases (as explained in the app description)
  • installing the regular Python package from python.org
Answered By: Benoit Blanchon
  1. Download GDAL wheel file that is supported for your platform from here.
  2. Open the command window where the downloaded file is located and activate your virtual environment.

Activating your virtual environment

  1. Then install the wheel using command pip install name_of_the_file .

Screenshot 2

You will see osgeo folder has been created in the location ‘…Envsmy_djangoLibsite-packages’ .

  1. Go to osgeo folder and copy the entire path of your gdalxxx.dll file and add to setting.py file as gdal library path. For example

    GDAL_LIBRARY_PATH = r'C:UsersWIN8Envsmy_djangoLibsite-packagesosgeogdal300.dll'

Answered By: Sougata Dolai

Just to follow up on the nice and detailed answer of Udi (cannot comment directly as my rep is under 50, it is the answer marked as the most useful);

After many hours I tried his offered solution which also did not work for me.
I was getting the following error:

OSError: [WinError 193] %1 is not a valid Win32 application

But I stayed there and found out that although I’m running 64 python and operating system (for sure), it kept looking for 32 bit (OSGeo4W) folder. What eventually let me pass was to copy contents of the OSGeo4W64 folder to the OSGeo4W. Hope it will save you time.

One more note:

Be sure that you edit libgdal.py file in your environment folder. It may exist in more than one place – your python folder and environment folder – if you edit the libgdal in your python dir, it is not going to work.

Answered By: David Louda

Creating the map compatible database

I recently got a new laptop and had to install Python and the related software on my new machine. I was trying to create a geodjango compatible database using OSGeo4W and had forgotten about the role of PostgreSQL and pgAdmin4.

When you install the latest PostgreSQL setup from https://www.enterprisedb.com/downloads/postgres-postgresql-downloads, you get pgAdmin4 at the same time.

Note – Ecommerce is my personal server group.

enter image description here

When you create a PostgreSQL 13 server, your server group will be different to mine.
enter image description here

When I first opened pgAdmin4 on my new machine, I kept the default username ‘postgres’. If when you first logged into pgAdmin4 you chose your own username, you will have to change the values below accordingly.
enter image description here

Connecting to the database

In my code I had

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"sharegdal"
os.environ['PROJ_LIB'] = OSGEO4W + r"shareproj"
os.environ['PATH'] = OSGEO4W + r"bin;" + os.environ['PATH']
GDAL_LIBRARY_PATH = r'C:OSGeo4W64bingdal300.dll'

I was getting an error message regarding the GDAL_LIBRARY_PATH so checked Windows Explorer. On discovering I did not have a gdal300.dll file, I changed my code to

GDAL_LIBRARY_PATH = r'C:OSGeo4W64bingdal301.dll'

enter image description here

Answered By: Ross Symonds
pip install GDAL

(From https://www.lfd.uci.edu/~gohlke/pythonlibs/)

Add In %PYTHON_ROOT%Libsite-packagesdjangocontribgisgdallibgdal.py

import osgeo

Check Gdal Vesion In: %PYTHON_ROOT%Libsite-packagesosgeogdal303.dll

lib_names = ['gdal303', 'gdal202', 'gdal201', 'gdal20', 'gdal111', 'gdal110', 'gdal19']
Answered By: Sunny Singh