How to Solve Error Message Could not find the GDAL library when running Django Application in Microsoft Windows

Posted on

Introduction

In this article, the main focus is just to be able to solve an error message appear upon running a Django application. The execution process of the Django application is in a device running using Microsoft Windows as its operating system. Before going to the part for solving the error message, below is the appearance of the command execution triggering the that error message :

(env) C:\repository\django\myproject>python manage.py runserver
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\python\python310\lib\threading.py", line 1016, in _bootstrap_inner
self.run()
File "C:\python\python310\lib\threading.py", line 953, in run
self._target(*self._args, **self._kwargs)
File "C:\repository\django\env\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\repository\django\env\lib\site-packages\django\core\management\commands\runserver.py", line 125, in inner_run
autoreload.raise_last_exception()
File "C:\repository\django\env\lib\site-packages\django\utils\autoreload.py", line 87, in raise_last_exception
raise _exception[1]
File "C:\repository\django\env\lib\site-packages\django\core\management\__init__.py", line 398, in execute
autoreload.check_errors(django.setup)()
File "C:\repository\django\env\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\repository\django\env\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\repository\django\env\lib\site-packages\django\apps\registry.py", line 116, in populate
app_config.import_models()C:\repository\django\
File "env\lib\site-packages\django\apps\config.py", line 304, in import_models
self.models_module = import_module(models_module_name)
File "C:\python\python310\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "C:\repository\django\env\lib\site-packages\django\contrib\auth\models.py", line 3, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "C:\repository\django\env\lib\site-packages\django\contrib\auth\base_user.py", line 49, in <module>
class AbstractBaseUser(models.Model):
File "C:\repository\django\env\lib\site-packages\django\db\models\base.py", line 141, in __new__
new_class.add_to_class("_meta", Options(meta, app_label))
File "C:\repository\django\env\lib\site-packages\django\db\models\base.py", line 369, in add_to_class
value.contribute_to_class(cls, name)
File "C:\repository\django\env\lib\site-packages\django\db\models\options.py", line 235, in contribute_to_class
self.db_table, connection.ops.max_name_length()
File "C:\repository\django\env\lib\site-packages\django\utils\connection.py", line 15, in __getattr__
return getattr(self._connections[self._alias], item)
File "C:\repository\django\env\lib\site-packages\django\utils\connection.py", line 62, in __getitem__
conn = self.create_connection(alias)
File "C:\repository\django\env\lib\site-packages\django\db\utils.py", line 208, in create_connection
backend = load_backend(db["ENGINE"])
File "C:\repository\django\env\lib\site-packages\django\db\utils.py", line 113, in load_backend
return import_module("%s.base" % backend_name)
File "C:\python\python310\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "C:\repository\django\env\lib\site-packages\django\contrib\gis\db\backends\postgis\base.py", line 6, in <module>
from .features import DatabaseFeatures
File "C:\repository\django\env\lib\site-packages\django\contrib\gis\db\backends\postgis\features.py", line 1, in <module>
from django.contrib.gis.db.backends.base.features import BaseSpatialFeatures
File "C:\repository\django\env\lib\site-packages\django\contrib\gis\db\backends\base\features.py", line 3, in <module>
from django.contrib.gis.db import models
File "C:\repository\django\env\lib\site-packages\django\contrib\gis\db\models\__init__.py", line 3, in <module>
import django.contrib.gis.db.models.functions # NOQA
File "C:\repository\django\env\lib\site-packages\django\contrib\gis\db\models\functions.py", line 3, in <module>
from django.contrib.gis.db.models.fields import BaseSpatialField, GeometryField
File "C:\repository\django\env\lib\site-packages\django\contrib\gis\db\models\fields.py", line 3, in <module>
from django.contrib.gis import forms, gdal
File "C:\repository\django\env\lib\site-packages\django\contrib\gis\forms\__init__.py", line 3, in <module>
from .fields import ( # NOQA
File "C:\repository\django\env\lib\site-packages\django\contrib\gis\forms\fields.py", line 2, in <module>
from django.contrib.gis.gdal import GDALException
File "C:\repository\django\env\lib\site-packages\django\contrib\gis\gdal\__init__.py", line 28, in <module>
from django.contrib.gis.gdal.datasource import DataSource
File "C:\repository\django\env\lib\site-packages\django\contrib\gis\gdal\datasource.py", line 40, in <module>
from django.contrib.gis.gdal.driver import Driver
File "C:\repository\django\env\lib\site-packages\django\contrib\gis\gdal\driver.py", line 5, in <module>
from django.contrib.gis.gdal.prototypes import ds as vcapi
File "C:\repository\django\env\lib\site-packages\django\contrib\gis\gdal\prototypes\ds.py", line 9, in <module>
from django.contrib.gis.gdal.libgdal import GDAL_VERSION, lgdal
File "C:\repository\django\env\lib\site-packages\django\contrib\gis\gdal\libgdal.py", line 62, in <module>
raise ImproperlyConfigured(
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.

(env) C:\repository\django\myproject>

What is the script in the Django application causing the error message above ?. Before getting in to the solution for solving the problem, below is the condition of the Django application :

  1. First of all, it is a project with the name of ‘myproject’. The following is the existence of the project :

    C:\repository>cd django
    
    C:\repository\django>dir
    C:\repository\django>cd myproject
     Volume in drive C is Windows-SSD 
     Volume Serial Number is CA30-19A4 
     Directory of C:\repository\django 
    
    07/22/2022 09:11 AM <DIR> . 
    07/17/2022 08:56 PM <DIR> .. 
    07/22/2022 09:09 AM <DIR> env 
    07/25/2022 04:13 PM <DIR> myproject 
                  0 File(s)  0 bytes 
                  4 Dir(s) 132,979,658,752 bytes free 
    
    C:\repository\django>
    
  2. Continue on in to the detail, in that project with the name of ‘myproject’, there is an application with the name of ‘djangomap’ as follows :

    C:\repository\django>cd myproject
    C:\repository\django\myproject>dir
    Volume in drive C is Windows-SSD
    Volume Serial Number is CA30-19A4
    
    Directory of C:\repository\django\myproject
    
    07/25/2022 04:13 PM <DIR> .
    07/22/2022 09:11 AM <DIR> ..
    07/25/2022 04:20 PM <DIR> djangomap
    07/22/2022 09:11 AM 687 manage.py
    07/25/2022 04:20 PM <DIR> myproject
    1 File(s) 687 bytes
    4 Dir(s) 132,968,116,224 bytes free
    
    C:\repository\django\myproject>
  3. Next, there is a model.py file inside the ‘djangomap’ folder. In the ‘models.py’ file, there is a declaration of a single class with the name of ‘City’ exist below :

    from django.db import models
    # Create your models here.
    class City(models.Model):
        name = models.CharField(max_length=100, blank=False)
        geometry = geomodels.PointField()
        class Meta:
            # order of drop down list items
            ordering = ('name',)
            # plural form in admin view
            verbose_name_plural = 'cities'
    

How to Solve Error Message Could not find the GDAL library when running Django Application

So, this part will show how to solve the error message. But solving the error message itself need several steps to do it. Those steps including the process for retrieving GDAL library into the local device. After that, the next step is to configure Django project in order to recognize and import the GDAL library for further execution. In that case, the following are the steps to achieve it :

  1. First of all, the first step is just to download the tool or application which has the GDAL library along with it. In this context, that application name is ‘QGIS’. Just download it in the official website of QGIS available in this link
  2. .Next step, it is installing the QGIS applicationi in order to get GDAL library and store it in the local device. So, after successfully downloading it, just execute it in order to install the QGIS application. As a reference, just read an article with the title of ‘How to Install QGIS Application in Microsoft Windows’ in this link in order to  install QGIS application.
  3. After getting the GDAL library in the local device, configure the Django project. Check the existence of the GDAL library before configuring it. In this context, the GDAL library exist in ‘C:\OSGeo4W\bin’ folder. Inside that folder, there is a DLL file with the name of ‘gdal305’ as exist below :
    C:\OSGeo4W\bin>dir
    Volume in drive C is Windows-SSD
    Volume Serial Number is CA30-19A4
    
    Directory of C:\OSGeo4W\bin
    
    07/25/2022 04:51 PM <DIR> .
    07/25/2022 04:51 PM <DIR> ..
    07/25/2022 04:46 PM <DIR> 1033
    07/13/2020 05:40 PM 1,833,000 adal.dll
    10/07/2020 07:42 PM 44,032 adrg.dll
    06/16/2022 06:24 PM 13,824 applygeo.exe
    ...
    ...
    ...
    07/07/2022 04:59 PM 4,659 gdal-config
    07/07/2022 05:19 PM 29,364,224 gdal305.dll
    07/07/2022 05:19 PM 235,520 gdaladdo.exe
    07/07/2022 05:19 PM 52,736 gdalbuildvrt.exe
    07/07/2022 05:19 PM 219,136 gdaldem.exe
    07/07/2022 05:19 PM 261,632 gdalenhance.exe
    07/07/2022 05:19 PM 52,224 gdalinfo.exe
    07/07/2022 05:19 PM 76,800 gdallocationinfo.exe
    07/07/2022 05:19 PM 61,440 gdalmanage.exe
    07/07/2022 05:19 PM 49,152 gdalmdiminfo.exe
    07/07/2022 05:19 PM 216,576 gdalmdimtranslate.exe
    07/07/2022 05:19 PM 227,840 gdalsrsinfo.exe
    07/07/2022 05:19 PM 75,264 gdaltindex.exe
    07/07/2022 05:19 PM 61,440 gdaltransform.exe
    07/07/2022 05:19 PM 77,824 gdalwarp.exe
    07/07/2022 05:19 PM 74,240 gdal_contour.exe
    07/07/2022 05:19 PM 227,840 gdal_create.exe
    07/07/2022 05:19 PM 49,664 gdal_grid.exe
    07/07/2022 05:19 PM 218,112 gdal_rasterize.exe
    07/07/2022 05:19 PM 222,208 gdal_translate.exe
    07/07/2022 05:19 PM 57,856 gdal_viewshed.exe
    ...
    ...
    ...
    224 File(s) 336,177,144 bytes
    3 Dir(s) 132,956,602,368 bytes free
    
    C:\OSGeo4W\bin>

    Apparently, there is a file with the name of ‘gdal305.dll’. That is the file which in this context will help to solve the problem and handle the error message.

  4. Following after, just open the ‘settings.py’ file which exist in the ‘myproject’ folder as follows :

    C:\repository\django\myproject> cd myproject
    C:\repository\django\myproject\myproject>dir
    Volume in drive C is Windows-SSD
    Volume Serial Number is CA30-19A4
    
    Directory of C:\repository\django\myproject\myproject
    
    07/25/2022 04:20 PM <DIR> .
    07/25/2022 04:13 PM <DIR> ..
    07/22/2022 09:11 AM 411 asgi.py
    07/25/2022 04:50 PM 3,705 settings.py
    07/22/2022 09:11 AM 772 urls.py
    07/22/2022 09:11 AM 411 wsgi.py
    07/22/2022 09:11 AM 0 __init__.py
    07/25/2022 04:50 PM <DIR> __pycache__
    5 File(s) 5,299 bytes
    3 Dir(s) 132,947,566,592 bytes free
    
    C:\repository\django\myproject\myproject>
    
  5. So, in order to solve the problem, just let Django project know the location of the GDAL library. How to do that ?. Just add a line for defining the path of the GDAL library as follows :

    GDAL_LIBRARY_PATH = "C:\OSGeo4W\\bin\gdal305"
  6. Save the file and then execute the project once more. If there are no further error appear, the solution for solving the error message is working.

Leave a Reply