Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I solve "django.core.exceptions.ImproperlyConfigured: Could not find the GDAL library" when running PyCharm test?

I'm using Django, Python 3.7. and PyCharm 2018.3.5. I have this test file ...

./web/tests/view_tests.py

It looks like the below

# Basic test to verify we can get valid return data
def test_calculate_tax(self):
    state = 'MN'
    gross = 100000
    salary = 75000
    json_data = json.dumps({'state': state,
                            'gross': gross,
                            'salary': salary})

    response = c.post('/content/vote/', json_data,
                      content_type='application/json',
                      HTTP_X_REQUESTED_WITH='XMLHttpRequest')
    self.assertEqual(response.status_code, 302)  # this is OK.
    print(response.content)
    self.assertEqual(response.content, 2)

When I right click the test name in PyCharm and select the "Run Test" option, though, I get the below error. I have no idea what it means or how to fix it ...

Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/helpers/pycharm/django_test_manage.py", line 56, in <module>
    django.setup()
  File "/Users/davea/Documents/workspace/myproject/venv/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/davea/Documents/workspace/myproject/venv/lib/python3.7/site-packages/django/apps/registry.py", line 114, in populate
    app_config.import_models()
  File "/Users/davea/Documents/workspace/myproject/venv/lib/python3.7/site-packages/django/apps/config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/davea/Documents/workspace/myproject/web/models.py", line 2, in <module>
    from django.contrib.gis.db import models
  File "/Users/davea/Documents/workspace/myproject/venv/lib/python3.7/site-packages/django/contrib/gis/db/models/__init__.py", line 3, in <module>
    import django.contrib.gis.db.models.functions  # NOQA
  File "/Users/davea/Documents/workspace/myproject/venv/lib/python3.7/site-packages/django/contrib/gis/db/models/functions.py", line 4, in <module>
    from django.contrib.gis.db.models.fields import BaseSpatialField, GeometryField
  File "/Users/davea/Documents/workspace/myproject/venv/lib/python3.7/site-packages/django/contrib/gis/db/models/fields.py", line 3, in <module>
    from django.contrib.gis import forms, gdal
  File "/Users/davea/Documents/workspace/myproject/venv/lib/python3.7/site-packages/django/contrib/gis/forms/__init__.py", line 3, in <module>
    from .fields import (  # NOQA
  File "/Users/davea/Documents/workspace/myproject/venv/lib/python3.7/site-packages/django/contrib/gis/forms/fields.py", line 2, in <module>
    from django.contrib.gis.gdal import GDALException
  File "/Users/davea/Documents/workspace/myproject/venv/lib/python3.7/site-packages/django/contrib/gis/gdal/__init__.py", line 28, in <module>
    from django.contrib.gis.gdal.datasource import DataSource
  File "/Users/davea/Documents/workspace/myproject/venv/lib/python3.7/site-packages/django/contrib/gis/gdal/datasource.py", line 39, in <module>
    from django.contrib.gis.gdal.driver import Driver
  File "/Users/davea/Documents/workspace/myproject/venv/lib/python3.7/site-packages/django/contrib/gis/gdal/driver.py", line 5, in <module>
    from django.contrib.gis.gdal.prototypes import ds as vcapi, raster as rcapi
  File "/Users/davea/Documents/workspace/myproject/venv/lib/python3.7/site-packages/django/contrib/gis/gdal/prototypes/ds.py", line 9, in <module>
    from django.contrib.gis.gdal.libgdal import GDAL_VERSION, lgdal
  File "/Users/davea/Documents/workspace/myproject/venv/lib/python3.7/site-packages/django/contrib/gis/gdal/libgdal.py", line 43, in <module>
    % '", "'.join(lib_names)
django.core.exceptions.ImproperlyConfigured: Could not find the GDAL library (tried "gdal", "GDAL", "gdal2.3.0", "gdal2.2.0", "gdal2.1.0", "gdal2.0.0", "gdal1.11.0"). Is GDAL installed? If it is, try setting GDAL_LIBRARY_PATH in your settings.

Any help figuring this out would be appreciated.

Edit: I think this is may be some side effect of my trying to include the USStateField as a model field? See below. I'm open to other ways of doing this.

from localflavor.us.models import USStateField
...
class UsLocation(models.Model):
    address_1 = models.CharField(_("address"), max_length=128)
    address_2 = models.CharField(_("address cont'd"), max_length=128, blank=True)

    city = models.CharField(_("city"), max_length=64, null=False)
    state = USStateField(_("state"), null=False)
    zip_code = models.CharField(_("zip code"), max_length=10, null=False)
like image 440
Dave Avatar asked Apr 10 '19 22:04

Dave


People also ask

Can't find the Gdal library tried?

ImportError: Could not find the GEOS library (tried "geos_c"). Try setting GEOS_LIBRARY_PATH in your settings. The most common solution is to properly configure your Library environment settings or set GEOS_LIBRARY_PATH in your settings.

What is Gdal Django?

GDAL stands for Geospatial Data Abstraction Library, and is a veritable “Swiss army knife” of GIS data functionality. A subset of GDAL is the OGR Simple Features Library, which specializes in reading and writing vector geographic data in a variety of standard formats.

How do I install GeoDjango?

First, download the OSGeo4W installer (64bit), and run it. Select Express Web-GIS Install and click next. In the 'Select Packages' list, ensure that GDAL is selected; MapServer is also enabled by default, but is not required by GeoDjango and may be unchecked safely.


2 Answers

In Ubuntu you can install GDAL library by running:

sudo apt-get install gdal-bin

This fixed the error.

like image 177
iraj jelodari Avatar answered Oct 04 '22 22:10

iraj jelodari


On Mac High Sierra, I ran

brew install gdal

and this fixed the issue.

like image 33
Dave Avatar answered Oct 05 '22 00:10

Dave