我正在使用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.6b w / python 3.3.2有关?
答案 0 :(得分:5)
确保您已安装Geospatial libraries。我在虚拟环境中测试了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
has been removed from 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
答案 1 :(得分:1)
我刚遇到同样的问题。不幸的是,当Django找不到GEOS库时,ImportError: cannot import name GEOSException
不是最好的错误消息。
我有Django 1.6.1 final和Python 3.3。操作系统是Windows 7 64位。 Python,GEOS也是x86-64。
根据Django docs的建议,行:
GEOS_LIBRARY_PATH = r'C:\Program Files\OSGeo4W64\bin\geos_c.dll'
在我的Django项目设置中解决了这个问题。