3

我正在使用 django 1.6b 和 python 3.3,我得到这个导入错误,

./manage.py runserver
Validating models...

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x104ae4d40>
Traceback (most recent call last):
  File "/opt/boxen/pyenv/versions/side-project/lib/python3.3/site-packages/django/utils/autoreload.py", line 93, in wrapper
    fn(*args, **kwargs)
  File "/opt/boxen/pyenv/versions/side-project/lib/python3.3/site-packages/django/core/management/commands/runserver.py", line 97, in inner_run
    self.validate(display_num_errors=True)
  File "/opt/boxen/pyenv/versions/side-project/lib/python3.3/site-packages/django/core/management/base.py", line 308, in validate
    num_errors = get_validation_errors(s, app)
  File "/opt/boxen/pyenv/versions/side-project/lib/python3.3/site-packages/django/core/management/validation.py", line 34, in get_validation_errors
    for (app_name, error) in get_app_errors().items():
  File "/opt/boxen/pyenv/versions/side-project/lib/python3.3/site-packages/django/db/models/loading.py", line 196, in get_app_errors
    self._populate()
  File "/opt/boxen/pyenv/version/side-project/lib/python3.3/site-packages/django/db/models/loading.py", line 78, in _populate
    self.load_app(app_name)
  File "/opt/boxen/pyenv/versions/side-project/lib/python3.3/site-packages/django/db/models/loading.py", line 99, in load_app
    models = import_module('.models', app_name)
  File "/opt/boxen/pyenv/versions/side-project/lib/python3.3/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/Users/moi/Projects/sos/dealerships/models.py", line 4, in <module>
    from django.contrib.gis.db import models # as geomodels
  File "/opt/boxen/pyenv/versions/side-project/lib/python3.3/site-packages/django/contrib/gis/db/models/__init__.py", line 8, in <module>
    from django.contrib.gis.db.models.manager import GeoManager
  File "/opt/boxen/pyenv/versions/side-project/lib/python3.3/site-packages/django/contrib/gis/db/models/manager.py", line 2, in <module>
    from django.contrib.gis.db.models.query import GeoQuerySet
  File "/opt/boxen/pyenv/versions/side-project/lib/python3.3/site-packages/django/contrib/gis/db/models/query.py", line 6, in <module>
    from django.contrib.gis.db.models.fields import get_srid_info, PointField, LineStringField
  File "/opt/boxen/pyenv/versions/side-project/lib/python3.3/site-packages/django/contrib/gis/db/models/fields.py", line 4, in <module>
    from django.contrib.gis import forms
  File "/opt/boxen/pyenv/versions/side-project/lib/python3.3/site-packages/django/contrib/gis/forms/__init__.py", line 2, in <module>
    from .fields import (GeometryField, GeometryCollectionField, PointField,
  File "/opt/boxen/pyenv/versions/side-project/lib/python3.3/site-packages/django/contrib/gis/forms/fields.py", line 11, in <module>
    from django.contrib.gis.geos import GEOSException, GEOSGeometry, fromstr
ImportError: cannot import name GEOSException

这是我为 gis 准备的唯一代码,

from django.contrib.gis.db import models as geomodels
from django.utils.translation import ugettext as _
from django.contrib.localflavor.us.models import USStateField

class UsLocation(geomodels.Model):
    address_1 = geomodels.CharField(_("address"), max_length=128)
    address_2 = geomodels.CharField(_("address cont'd"), max_length=128, blank=True)

    city = geomodels.CharField(_("city"), max_length=64, default="Kansas City")
    state = USStateField(_("state"), default="KO")
    zip_code = geomodels.CharField(_("zip code"), max_length=5, default="16874")

现在我不会为我的地址模型使用 gis 助手,但我不确定是什么导致了这个导入异常。我检查了 django 1.5/1.6 版本,它们具有相同的行。我想知道它是否与我使用 1.6bw/python 3.3.2 有关?

4

2 回答 2

5

确保您已安装地理空间库。我已经在虚拟环境中测试了 Python 3.3.2 和 Django 1.6 beta 2。contrib.gis从工作正常的导入。

>>> import django
>>> django.VERSION
(1, 6, 0, 'beta', 2)
>>> from django.contrib.gis.db import models as geomodels

请注意,contrib.localflavor 已从 Django 1.6 中删除

>>> from django.contrib.localflavor.us.models import USStateField
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ImportError: No module named 'django.contrib.localflavor'

您可以django-localflavor使用以下命令安装软件包:

pip install django-localflavor

然后将您的导入更改为:

from localflavor.us.models import USStateField
于 2013-08-14T19:30:59.930 回答
1

我刚刚遇到了同样的问题。不幸的是,ImportError: cannot import name GEOSException当 Django 找不到 GEOS 库时,这不是最好的错误消息。

我也有 Django 1.6.1 final 和 Python 3.3。操作系统是 Windows 7 64 位。Python、GEOS 也是 x86-64。

正如 Django 文档所推荐的那样,该行:

GEOS_LIBRARY_PATH = r'C:\Program Files\OSGeo4W64\bin\geos_c.dll'

在我的 Django 项目设置中解决了这个问题。

于 2014-04-12T12:44:30.457 回答