Introduction
This article is focusing to discuss about how to solve a specific error message. That error message is exist as part of the title of this article. Furthermore, the error message appear in the middle of the Django application’s execution. In detail, the following is the actual execution of the Django application :
(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() File "C:\repository\django\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\myproject\djangomap\models.py", line 2, in <module> from django.contrib.gis.db import models as geomodels 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 3, in <module> from django.contrib.gis.geos import GEOSException, GEOSGeometry File "C:\repository\django\env\lib\site-packages\django\contrib\gis\geos\__init__.py", line 5, in <module> from .collections import ( # NOQA File "C:\repository\django\env\lib\site-packages\django\contrib\gis\geos\collections.py", line 6, in <module> from django.contrib.gis.geos.geometry import GEOSGeometry, LinearGeometryMixin File "C:\repository\django\env\lib\site-packages\django\contrib\gis\geos\geometry.py", line 17, in <module> from django.contrib.gis.geos.prototypes.io import ewkb_w, wkb_r, wkb_w, wkt_r, wkt_w File "C:\repository\django\env\lib\site-packages\django\contrib\gis\geos\prototypes\io.py", line 234, in <module> class WKBWriter(IOBase): File "C:\repository\django\env\lib\site-packages\django\contrib\gis\geos\prototypes\io.py", line 238, in WKBWriter geos_version = geos_version_tuple() File "C:\repository\django\env\lib\site-packages\django\contrib\gis\geos\libgeos.py", line 174, in geos_version_tuple return get_version_tuple(geos_version().decode()) File "C:\repository\django\env\lib\site-packages\django\contrib\gis\geos\libgeos.py", line 169, in geos_version return lgeos.GEOSversion() File "C:\repository\django\env\lib\site-packages\django\utils\functional.py", line 258, in inner self._setup() File "C:\repository\django\env\lib\site-packages\django\utils\functional.py", line 398, in _setup self._wrapped = self._setupfunc() File "C:\repository\django\env\lib\site-packages\django\contrib\gis\geos\libgeos.py", line 53, in load_geos raise ImportError( ImportError: Could not find the GEOS library (tried "geos_c", "libgeos_c-1"). Try setting GEOS_LIBRARY_PATH in your settings.
Before going on to the solution in the next part, what exactly the Django project looks like. It is important to get know the Django project first before getting on to the detail of the solution. There maybe an important information which is describing about how the Django project trigger the error message. So, the following are the Django project description :
-
First of all, there is a Django project with the name of ‘myproject’. It exist as in the following output :
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>
-
The second information, there is also an application with the name of ‘djangomap’ as exist below :
C:\repository\django\myproject>cd djangomap C:\repository\django\myproject\djangomap>dir Volume in drive C is Windows-SSD Volume Serial Number is CA30-19A4 Directory of C:\repository\django\myproject\djangomap 07/25/2022 04:20 PM <DIR> . 07/25/2022 04:13 PM <DIR> .. 07/25/2022 04:13 PM 66 admin.py 07/25/2022 04:13 PM 156 apps.py 07/31/2022 02:52 PM <DIR> migrations 07/31/2022 02:46 PM 422 models.py 07/25/2022 04:13 PM 63 tests.py 07/25/2022 04:13 PM 66 views.py 07/25/2022 04:13 PM 0 __init__.py 07/31/2022 02:52 PM <DIR> __pycache__ 6 File(s) 773 bytes 4 Dir(s) 132,821,172,224 bytes free C:\repository\django\myproject\djangomap>
-
And in the third one, it has a script which is using the GEO library package. It is available inside the ‘djangomap’ folder application with the script name of ‘models.py’ as follows :
C:\repository\django\myproject> cd djangomap C:\repository\django\myproject\djangomap>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/31/2022 02:53 PM 3,929 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/31/2022 02:53 PM <DIR> __pycache__ 5 File(s) 5,523 bytes 3 Dir(s) 132,830,490,624 bytes free C:\repository\django\myproject\myproject>
-
Before going further, the following is the content of the ‘models.py’ script : Specifically, it exist in the ‘DATABASES’ configuration setting part. The following is content of that part :
from django.db import models from django.contrib.gis.db import models as geomodels # 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'
Why is it so important to check the ‘models.py’ script ?. The reason is because the error message itself exist and it is pointing the location of the trigger as in this following line :
File "C:\repository\django\myproject\djangomap\models.py", line 2, in from django.contrib.gis.db import models as geomodels
So, that line is triggering the error message and there need to be a way to handle it by actually introduce the models available in ‘django.contrib.gis.db’ to the Django project.
-
Last but not least, according to the information above, the cause of the error actually exist inside in the ‘models.py’ file. Another important part for giving an additional information is that the package with the name of ‘django.contrib.gis’ is already exist. It exist as a definition for further addition as an app in the following configuration part of the ‘settings.py’ :
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.gis', 'djangomap.apps.DjangomapConfig', ]
How to Solve Error Message Could not find the GEOS library (tried “geos_c”, “libgeos_c-1”)
Actually, the solution for the problem is exist as part of the error message. It is giving the solution of ‘Try setting GEOS_LIBRARY_PATH in your settings.’. So, the script which exist as part of the Django application actually need GEOS library. So, in order to do that, just add GEOS library, check the location and then define it in the ‘settings.py’ file. According to the sequence of the steps, the following is the actual execution of each step :
-
First of all, adding the GEOS library in the local device. It is possible by performing one simple solution. That simple solution is installing a QGIS application. Just look at an article with the title of ‘How to Install QGIS Application in Microsoft Windows’ in this link. It is an article showing the process for installing QGIS application.
-
After successfully installing the QGIS application. Check whether the GEOS library is available inside of it or not. In this context, the QGIS application exist in the default folder location. It is available in ‘C:\OSGeo4W’. Basically, all the DLL file or library files exist in the bin folder 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 ... 06/16/2022 12:08 AM 19,968 geod.exe 06/07/2022 08:05 PM 1,897,472 geos.dll 06/07/2022 08:06 PM 357,376 geos_c.dll 06/16/2022 06:24 PM 33,280 geotifcp.exe 06/16/2022 06:24 PM 160,256 geotiff.dll ... 224 File(s) 336,177,144 bytes 3 Dir(s) 132,821,196,800 bytes free C:\OSGeo4W\bin>
-
Since the DLL file or the library file which is important for executing the Django application properly exist, just define it in the Django project. Just open ‘settings.py’ file which exist in the project folder. In this context, just get in to the ‘myproject’ folder as it exist in the previous part. In order to recognize the new additional configuration line, just add the following configuration line to the ‘settings.py’ file as follows :
GEOS_LIBRARY_PATH = "C:\OSGeo4W\\bin\geos_c.dll"
-
After successfully add the above configuration line, just execute the Django application once more. If no more errors appear, the Django project will have a Django application running properly.