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

5

26

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)
Groping answered 10/4, 2019 at 22:15 Comment(0)
G
25

On Mac High Sierra, I ran

brew install gdal

and this fixed the issue.

Groping answered 16/4, 2019 at 22:19 Comment(3)
This destroyed my entire python environment and all my venvs. Beware.Pori
In case it helps someone, this was failing for me when it was trying to install a dependency of gdal i.e [email protected] (I was on 3.8). It said Target /usr/local/bin/2to3 already exists. Removing this file was the trick - the installation went through fine. (I am on MacOS Mojave)Embryology
Homebrew is absolutely horrible for nuking dev environments. If a warning was given, it'd be ok, but it just deletes or upgrades pythons almost at random and the homebrew devs are borderline hostile at anyone questioning it. I wish there was a sane alternative.Alienable
G
48

In Ubuntu you can install GDAL library by running:

sudo apt-get install gdal-bin

This fixed the error.

Guesthouse answered 16/6, 2019 at 14:49 Comment(3)
may require sudo apt-get install build-essential in order for install to succeedDruggist
For an Ubuntu system, I received this after installing PostGIS on my database. I had to run this command: sudo apt-get install binutils libproj-dev gdal-bin which is explained in the Django documentation when installing geospatial libraries: docs.djangoproject.com/en/dev/ref/contrib/gis/install/geolibs/…Posturize
What is the fix for Windows? (for Ubuntu, this worked for me)Streetwalker
G
25

On Mac High Sierra, I ran

brew install gdal

and this fixed the issue.

Groping answered 16/4, 2019 at 22:19 Comment(3)
This destroyed my entire python environment and all my venvs. Beware.Pori
In case it helps someone, this was failing for me when it was trying to install a dependency of gdal i.e [email protected] (I was on 3.8). It said Target /usr/local/bin/2to3 already exists. Removing this file was the trick - the installation went through fine. (I am on MacOS Mojave)Embryology
Homebrew is absolutely horrible for nuking dev environments. If a warning was given, it'd be ok, but it just deletes or upgrades pythons almost at random and the homebrew devs are borderline hostile at anyone questioning it. I wish there was a sane alternative.Alienable
P
2

For Mac OS

Run brew install gdal If you already have it installed, and are still facing the issue, I did the following.

Run

brew info gdal geos

This will list the current path where they are installed and their dependencies. Something like this,

==> gdal: stable 3.7.2 (bottled), HEAD
Geospatial Data Abstraction Library
https://www.gdal.org/
Conflicts with:
  avce00 (because both install a cpl_conv.h header)
  cpl (because both install cpl_error.h)
/opt/homebrew/Cellar/gdal/3.7.2 (474 files, 27.9MB) *
  Poured from bottle using the formulae.brew.sh API on 2023-09-25 at 13:35:19
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/g/gdal.rb
License: MIT
==> Dependencies
Build: boost ✔, cmake ✔, pkg-config ✘, swig ✘
Required: apache-arrow ✔, cfitsio ✔, epsilon ✔, expat ✔, freexl ✔, geos ✔, giflib ✔, hdf5 ✔, jpeg-turbo ✔, jpeg-xl ✔, json-c ✔, libarchive ✔, libgeotiff ✔, libheif ✔, libkml ✔, liblerc ✔, libpng ✔, libpq ✔, libspatialite ✔, libtiff ✔, libxml2 ✔, netcdf ✔, numpy ✘, openexr ✔, openjpeg ✔, openssl@3 ✘, pcre2 ✔, poppler ✔, proj ✔, [email protected] ✔, qhull ✔, sqlite ✔, unixodbc ✔, webp ✘, xerces-c ✔, xz ✔, zstd ✔
==> Options
--HEAD
    Install HEAD version
==> Analytics
install: 14,204 (30 days), 33,238 (90 days), 65,053 (365 days)
install-on-request: 11,665 (30 days), 27,230 (90 days), 53,906 (365 days)
build-error: 90 (30 days)

==> geos: stable 3.12.0 (bottled)
Geometry Engine
https://libgeos.org/
/opt/homebrew/Cellar/geos/3.12.0 (491 files, 13.7MB) *
  Poured from bottle using the formulae.brew.sh API on 2023-08-23 at 12:46:05
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/g/geos.rb
License: LGPL-2.1-or-later
==> Dependencies
Build: cmake ✔
==> Analytics
install: 4,065 (30 days), 14,532 (90 days), 31,010 (365 days)
install-on-request: 1,753 (30 days), 6,409 (90 days), 13,380 (365 days)
build-error: 0 (30 days)

For all the packages marked x, run brew install individually.

After that add GDAL_LIBRARY_PATH = '/opt/homebrew/Cellar/gdal/3.7.2/lib/libgdal.dylib' GEOS_LIBRARY_PATH = '/opt/homebrew/Cellar/geos/3.12.0/lib/libgeos_c.dylib'

to your settings.py (Exact path may differ with version)

Phyllisphylloclade answered 25/9, 2023 at 10:1 Comment(0)
R
0

On macOS Sonoma simple brew install didn't help, but creating symlinks did.

brew install gdal

And then:

# Replace the versions in your folder names

cd /usr/local/lib
sudo ln -s /opt/homebrew/Cellar/gdal/3.5.0_1/lib/libgdal.dylib libgdal.dylib
sudo ln -s /opt/homebrew/Cellar/geos/3.11.0/lib/libgeos_c.dylib libgeos_c.dylib
Rejoice answered 29/5 at 9:19 Comment(0)
S
-1

in poetry:

poetry shell

sudo apt-get install gdal-bin

Streamlet answered 28/10, 2022 at 14:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.