1

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.

Magnus
  • 11
  • 1
  • This link may be of use: https://www.pointsnorthgis.ca/blog/geodjango-gdal-setup-windows-10/ Note the part where you literally have to go into the environment site package library for gdal and edit the libgdal.py file for 'nt'. Lib\site-packages\django\contrib\gis\gdal\libgdal.py You would probably need to add 'gdal333' since it looks like that is the version that you are using. – Scott Jul 29 '22 at 17:20
  • Actually, in my comment above, it should be 'gdal303' as the previous versions follow a pattern if you look at the list of lib_names. – Scott Jul 29 '22 at 18:20

1 Answers1

0

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']
Andrew Ryan
  • 1,489
  • 3
  • 15
  • 21
  • I just tried this, and it still gives me the error django.core.exceptions.ImproperlyConfigured: Could not find the GDAL library (tried "gdal304", "gdal303", "gdal302", "gdal301", "gdal300", "gdal204", "gdal203", "gdal202"). Is GDAL installed? If it is, try setting GDAL_LIBRARY_PATH in your settings. – Magnus Oct 14 '22 at 16:53
  • Friend, did you manage to solve the error that you are getting, I have the same problem but I am using anaconda, do you know how we can solve it? – Carlos Chavita Nov 18 '22 at 13:27
  • just add gdal path to django settings file – abdalwhab salah Nov 19 '22 at 15:27