Search code examples
djangowindows-10gdalgeodjango

How to configure geodjango on windows10?


I am working on a geodjango project on windows-10. I have been following the tutorial and the installation guide from the geodjango documentation. I have installed all required packages and executed the bat script.

Python version: Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:13:57) [MSC v.1916 64 bit (AMD64)]

OSGEO version: GDAL 2.4.1, released 2019/03/15

Initially I had to reconfigure my virtual environment to have a 64-bit python version matching that of the OSGEOW4 application.

I've tried the solutions answered for questions 1 and 2.

Those solutions include:

  • Setting the environment variables before at the beginning of the settings.py of the django project

  • Defining the GDAL_LIBRARY_PATH

  • Adding the gdal version to the django\contrib\gis\gdal\libgdal.py

I have not yet tried the alternative of installing the gdal from a binary file. I do not understand why I would not be able to configure with the OSGEO application.

The error present in the command prompt when executing python manage.py check is:

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()

[...]

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

Seems like it is an issue that many encounter.

A concise yet complete answer would be greatly appreciated.

Thanks for the help.


Solution

  • I ended up downloading the binary GDAL file from this link. The version that worked for my Python version was GDAL-2.4.1-cp37-cp37m-win32.

    Some key points if you are stumbling upon the same issue:

    • Make sure the python version in your virtual environment is the same bit size as the gdal version

    • to enable execution of .bat scripts you must change the Set-ExecutionPolicy variable

    The steps of my configuration are the following:

    1. Download postgres with postgis spatial extension from edm
    2. Create Django project with a specific environment
    3. Open powershell and navigate to Django project environment directory
    cd .../your-venv/
    
    1. Set the execution policy variable
    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
    
    1. Activate the environment:
    Scripts/Activate.ps1
    
    1. Download the appropriate gdal version for your environment (https://www.lfd.uci.edu/~gohlke/pythonlibs/)
    2. Install gdal from binaries in your venv:
    pip install C:\...\GDAL-2.4.1-cp37-cp37m-win32.whl
    
    1. Install any other requirements for your project

    Hope this helps.