运行PyCharm测试时,如何解决“ django.core.exceptions.ImproperlyConfigured:找不到GDAL库”?

时间:2019-04-10 22:15:47

标签: django python-3.x unit-testing configuration pycharm

我正在使用Django,Python 3.7。和PyCharm 2018.3.5。我有这个测试文件...

./web/tests/view_tests.py

它看起来像下面的

# 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)

当我右键单击PyCharm中的测试名称并选择“运行测试”选项时,出现以下错误。我不知道这意味着什么或如何解决...

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.

任何帮助弄清楚这一点的人都会感激的。

编辑:我认为这可能是我尝试将USStateField包含为模型字段的某些副作用吗?见下文。我愿意采取其他方式。

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)

3 个答案:

答案 0 :(得分:2)

在Ubuntu中,您可以通过运行以下命令安装GDAL库

sudo apt-get install gdal-bin

这解决了错误。

答案 1 :(得分:0)

签出this页面。看来有些人遇到了这个问题,要么不得不重新安装软件包,要么通过自制软件重新安装。

答案 2 :(得分:0)

On Mac High Sierra, I ran

brew install gdal

and this fixed the issue.