29

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

Udi
  • 29,222
  • 9
  • 96
  • 129
Yorian
  • 2,002
  • 5
  • 34
  • 60

11 Answers11

82

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.

Udi
  • 29,222
  • 9
  • 96
  • 129
  • 4
    Udi, you are the hero of the day. It seems to work now , thank you! – Yorian Mar 08 '18 at 08:25
  • 5
    Udi you saved me from madness – Marc Laugharn Dec 03 '18 at 20:24
  • 4
    It worked, but would be also important to say that the code must be placed at the beginning of the `settings.py` file. – drec4s Aug 01 '19 at 15:42
  • I am getting this error message File "C:\Users\HP\django_project3\django_project3\settings.py", line 22, in assert os.path.isdir(OSGEO4W), "Directory does not exist: " + OSGEO4W AssertionError: Directory does not exist: C:\OSGeo4W64 – Ross Symonds Jun 05 '20 at 06:12
  • I had installed the 32 bit version by mistake. – Ross Symonds Jun 05 '20 at 06:33
  • giving this error; the procedure entry point sqlite2_column_origin_name could not be located in the dynamic link library c:\OSGeo4W64\bin\gdal301.dll – Irfan wani May 24 '21 at 05:58
  • Tried this but does not seem to work. I still get the same issue, see question asked on GIS Stack exchange https://gis.stackexchange.com/questions/407905/django-core-exceptions-improperlyconfigured-could-not-find-the-gdal-library-t – Amelia Nicodemus Aug 11 '21 at 09:41
  • @RossSymonds I am also facing the same error. can you help me to install the 64 bit version? btw have you managed to resolve the issue? – djangodeveloper Jul 24 '22 at 08:40
31

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']
kingurr
  • 431
  • 4
  • 6
  • 2
    You are the best, finally something works for me, thanks a lot! – Brayan Garcia Jun 14 '20 at 04:03
  • 2
    short note: for python 3.8 add `gdal301` as the lib_names. – dhiraj Jul 01 '20 at 04:37
  • 2
    And for Python 3.9, install `GDAL‑3.2.1‑cp39‑cp39‑win_amd64.whl` from the binaries site and add `gdal302` to the `lib_names` list. – Tom Jan 27 '21 at 09:02
  • Adding 'gdal301' to the lib_names in libgdal.py solved my problem. – Rene May 12 '21 at 12:29
  • I am getting KeyError: 'VIRTUAL_ENV' this error how can i get rid off from this i am following your tutorial. I have also posted this error as separate question on stackoverflow. – Filbadeha Aug 15 '21 at 08:54
  • 1
    After following the instructions in the answer above I was getting the error "django.core.exceptions.ImproperlyConfigured: Could not find the GDAL library (tried "gdal303", "gdal302", "gdal301", "gdal300", "gdal204", "gdal203", "gdal202", "gdal201", "gdal20"). Is GDAL installed? If it is, try setting GDAL_LIBRARY_PATH in your settings." and it turns out my library was named "gdal304" and once I added it to libgdal.py it started working. Thank you! – Lehrian Jan 23 '22 at 03:23
7

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:\OSGeo4W\bin and tried to "enrich" the list variable named lib_names with 'gdal204', at line 24 (regarding Windows NT shared libraries) of %PYTHON_ROOT%\Lib\site-packages\django\contrib\gis\gdal\libgdal.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.

keepAlive
  • 6,369
  • 5
  • 24
  • 39
  • I was about to say that. This could be a very small improvement in the Python binding, is there a way to add a suggestion / commit to this somewhere? – Olivier Pons Aug 19 '19 at 09:12
  • @OlivierPons you can [request this improvement on the ticket tracker](https://docs.djangoproject.com/en/dev/internals/contributing/bugs-and-features/). But I think that it will naturally be integrated in future versions of Django, sooner or later. – keepAlive Aug 19 '19 at 10:43
3

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 env\Lib\site-packages\django\contrib\gis\gdal\libgdal.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'env\Lib\site-packages\osgeo') + ';' + os.environ['PATH']

os.environ['PROJ_LIB'] = os.path.join(BASE_DIR, r'env3\Lib\site-packages\osgeo\data\proj') + ';' + os.environ['PATH']

GDAL_LIBRARY_PATH = os.path.join(BASE_DIR, r'env\Lib\site-packages\osgeo\gdal203.dll') 

In this case, env is my Python environment.

user42121
  • 31
  • 2
1

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
Benoit Blanchon
  • 13,364
  • 4
  • 73
  • 81
1
  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 '...\Envs\my_django\Lib\site-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:\Users\WIN8\Envs\my_django\Lib\site-packages\osgeo\gdal300.dll'

Jan Černý
  • 1,268
  • 2
  • 17
  • 31
0

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:\OSGeo4W\bin\gdal300'
  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%\share\gdal 
set PROJ_LIB=%OSGEO4W_ROOT%\share\proj
m02ph3u5
  • 3,022
  • 7
  • 38
  • 51
karthik k
  • 61
  • 1
  • 2
0

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.

David Louda
  • 498
  • 5
  • 19
0

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"\share\gdal"
os.environ['PROJ_LIB'] = OSGEO4W + r"\share\proj"
os.environ['PATH'] = OSGEO4W + r"\bin;" + os.environ['PATH']
GDAL_LIBRARY_PATH = r'C:\OSGeo4W64\bin\gdal300.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:\OSGeo4W64\bin\gdal301.dll'

enter image description here

0
pip install GDAL

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

Add In %PYTHON_ROOT%\Lib\site-packages\django\contrib\gis\gdal\libgdal.py

import osgeo

Check Gdal Vesion In: %PYTHON_ROOT%\Lib\site-packages\osgeo\gdal303.dll

lib_names = ['gdal303', 'gdal202', 'gdal201', 'gdal20', 'gdal111', 'gdal110', 'gdal19']
Sven Eberth
  • 3,057
  • 12
  • 24
  • 29
0

Here is a solution to this gdal problem in Django: https://www.dark-hamster.com/application/how-to-solve-error-message-could-not-find-the-gdal-library-when-running-django-application-in-microsoft-windows/?utm_source=pocket_saves.

  1. First of all ensure that you have downloaded the OSGeo4W package from here. Install it to your computer. It will take quite some time since the full package is >1GB.

  2. Ensure that you have a folder C:\OSGeo4W in your C Drive.

  3. Go to C:\OSGeo4W\bin and check for a file such as gdal306.dll. In my case I have gdal306.dll and gdal307.dll. I went with the former.

  4. Go to your settings.py file in your Django project.

  5. If you pasted your Django error somewhere, it read something like this:

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.

We will give it a taste of its own medicine by creating a variable GDAL_LIBRARY_PATH in your settings.py file.

  1. Copy the path to your gdal306.dll file from OSGeo4W folder. In my case it was, C:\OSGeo4W\bin\gdal306.dll. Paste it as the path to the GDAL_LIBRARY_PATH like so:

GDAL_LIBRARY_PATH = C:\OSGeo4W\bin\gdal306.dll

  1. Run your django project once more, if all is ok, there shouldn't be that error again.