user772401
user772401

Reputation: 2904

Django 1.6b gis import error

I'm using django 1.6b and python 3.3 and I get this import error,

./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

This is the only code I have in place for 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")

For now I'm going to not use the gis helpers for my address model but I'm not sure what's causing this import exception. I checked the django 1.5/1.6 releases and they have the same lines. I wonder if it has something to do with me using 1.6b w/ python 3.3.2 ?

Upvotes: 3

Views: 5400

Answers (2)

Alasdair
Alasdair

Reputation: 309099

Make sure you have installed the Geospatial libraries. I have tested Python 3.3.2 and Django 1.6 beta 2 in a virtual env. The import from contrib.gis works fine.

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

Note that 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'

You can install the django-localflavor package with:

pip install django-localflavor

then change your import to:

from localflavor.us.models import USStateField

Upvotes: 5

George Sovetov
George Sovetov

Reputation: 5238

I have just faced the same problem. Unfortunately, ImportError: cannot import name GEOSException is not the best error message when Django cannot find GEOS library.

I have Django 1.6.1 final and Python 3.3 too. OS is Windows 7 64-bit. Python, GEOS are x86-64 too.

As recommended at Django docs, the line:

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

in my Django project settings solved this issue.

Upvotes: 1

Related Questions