Problems with Geodjango and Gdal

Notes:

Using Django 4.0.5, and Python 3.10

IDE: Pycharm Professional

OSGEO4W version: 2 (https://download.osgeo.org/osgeo4w/v2/)

Problem

I cant get the gis library to work in Django. I followed the documentation: https://docs.djangoproject.com/en/4.0/ref/contrib/gis/install/#windows. Having followed these steps I get this error:

django.core.exceptions.ImproperlyConfigured: Could not find the GDAL library (tried "gdal303", "gdal302", "gdal301", "gdal300", "gdal204", "gdal203", "gdal202", "gdal201", "gdal20")

I then looked into what version of GDAL, that OSGEO4W had installed and found it to be gdal305. So I went in on https://www.lfd.uci.edu/~gohlke/pythonlibs/#gdal, where I downloaded the wheel, for my version of python, (GDAL-3.3.3-cp310-cp310-win_amd64.whl) and used the command pip install GDAL-3.3.3-cp310-cp310-win_amd64.whl. I then changed the path variables to go for this version of gdal rather then the one installed with OSgeo4W. I then got the following error: OSError: [WinError 127] : The specified procedure could not be found

After reading online, I found the following stackoverflow: OSError in Geodjango: [WinError 127] : The specified procedure could not be found

However, did this not work for me, I still get the same errors (depending on where i set the gdal path).

Other things i tried:

  1. I tried to use an earlier version of OSgeo4W, but the installer did not have any available download sites.

  2. I tried using anaconda instead of pycharm venv, and downloading gdal, proj, geos through conda-forge.

If more information is needed please let me know. First time writing on stackoverflow.

hi run this in cmd or terminal after open it as (run as admin):

set OSGEO4W_ROOT=C:\OSGeo4W set PYTHON_ROOT=C:\Python3X set
 GDAL_DATA=%OSGEO4W_ROOT%\share\gdal set
 PROJ_LIB=%OSGEO4W_ROOT%\share\proj set
 PATH=%PATH%;%PYTHON_ROOT%;%OSGEO4W_ROOT%\bin 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%" 

then add this to the code

 settings.py import os BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


 # use this if setting up on Windows 10 with GDAL installed from OSGeo4W using defaults 
 if os.name == 'nt':
      VIRTUAL_ENV_BASE = os.environ['VIRTUAL_ENV']
      os.environ['PATH'] = os.path.join(VIRTUAL_ENV_BASE, r'.\Lib\site-packages\osgeo') + ';' + os.environ['PATH']
      os.environ['PROJ_LIB'] = os.path.join(VIRTUAL_ENV_BASE, r'.\Lib\site-packages\osgeo\data\proj') + ';' + os.environ['PATH']
Back to Top