Django AppRegistryNotReady Exception after import from django.contrib.auth.models - django

Here is my folder structure:
.
├── app
├── project
├── scripts
└── tests
├── fuctional -> test_views.py
└── unit
Here is my test_script.py
from django.test import TestCase
from django.urls import reverse
from django.contrib.auth.models import User
from django.contrib.auth.hashers import make_password
from rest_framework.test import APIClient
from mrx.views import LoginView, LogoutView
When I launch python -m unittest tests/fuctional/test_views.py I get this error:
Traceback (most recent call last):
File "/home/edx/.pyenv/versions/3.8.1/lib/python3.8/runpy.py", line 193, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/home/edx/.pyenv/versions/3.8.1/lib/python3.8/runpy.py", line 86, in _run_code
exec(code, run_globals)
File "/home/edx/.pyenv/versions/3.8.1/lib/python3.8/unittest/__main__.py", line 18, in <module>
main(module=None)
File "/home/edx/.pyenv/versions/3.8.1/lib/python3.8/unittest/main.py", line 100, in __init__
self.parseArgs(argv)
File "/home/edx/.pyenv/versions/3.8.1/lib/python3.8/unittest/main.py", line 147, in parseArgs
self.createTests()
File "/home/edx/.pyenv/versions/3.8.1/lib/python3.8/unittest/main.py", line 158, in createTests
self.test = self.testLoader.loadTestsFromNames(self.testNames,
File "/home/edx/.pyenv/versions/3.8.1/lib/python3.8/unittest/loader.py", line 220, in loadTestsFromNames
suites = [self.loadTestsFromName(name, module) for name in names]
File "/home/edx/.pyenv/versions/3.8.1/lib/python3.8/unittest/loader.py", line 220, in <listcomp>
suites = [self.loadTestsFromName(name, module) for name in names]
File "/home/edx/.pyenv/versions/3.8.1/lib/python3.8/unittest/loader.py", line 154, in loadTestsFromName
module = __import__(module_name)
File "/home/edx/PycharmProjects/mrx_3/tests/fuctional/test_views.py", line 3, in <module>
from django.contrib.auth.models import User
File "/home/edx/.pyenv/versions/MRX/lib/python3.8/site-packages/django/contrib/auth/models.py", line 2, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/home/edx/.pyenv/versions/MRX/lib/python3.8/site-packages/django/contrib/auth/base_user.py", line 48, in <module>
class AbstractBaseUser(models.Model):
File "/home/edx/.pyenv/versions/MRX/lib/python3.8/site-packages/django/db/models/base.py", line 108, in __new__
app_config = apps.get_containing_app_config(module)
File "/home/edx/.pyenv/versions/MRX/lib/python3.8/site-packages/django/apps/registry.py", line 253, in get_containing_app_config
self.check_apps_ready()
File "/home/edx/.pyenv/versions/MRX/lib/python3.8/site-packages/django/apps/registry.py", line 136, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
I've tried with import Django and then django.setup() and it works but:
why I can't import from django.contrib.auth.models import User ?
How do you test outside a Django app code inside a Django app?
Edit:
I've also tried with pytest, using init.py with import Django and then django.setup() but I still got an error:
(MRX) edx#edx-VirtualBox:~/PycharmProjects/mrx_3$ pytest tests/fuctional/test_views.py
=========================================================================================== test session starts ===========================================================================================
platform linux -- Python 3.8.1, pytest-6.2.1, py-1.10.0, pluggy-0.13.1
rootdir: /home/edx/PycharmProjects/mrx_3
collected 0 items / 1 error
================================================================================================= ERRORS ==================================================================================================
_____________________________________________________________________________ ERROR collecting tests/fuctional/test_views.py ______________________________________________________________________________
tests/fuctional/test_views.py:7: in <module>
from django.contrib.auth.models import User
../../.pyenv/versions/3.8.1/envs/MRX/lib/python3.8/site-packages/django/contrib/auth/models.py:2: in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
../../.pyenv/versions/3.8.1/envs/MRX/lib/python3.8/site-packages/django/contrib/auth/base_user.py:48: in <module>
class AbstractBaseUser(models.Model):
../../.pyenv/versions/3.8.1/envs/MRX/lib/python3.8/site-packages/django/db/models/base.py:108: in __new__
app_config = apps.get_containing_app_config(module)
../../.pyenv/versions/3.8.1/envs/MRX/lib/python3.8/site-packages/django/apps/registry.py:253: in get_containing_app_config
self.check_apps_ready()
../../.pyenv/versions/3.8.1/envs/MRX/lib/python3.8/site-packages/django/apps/registry.py:136: in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
E django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
========================================================================================= short test summary info =========================================================================================
ERROR tests/fuctional/test_views.py - django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
============================================================================================ 1 error in 0.95s =============================================================================================
It seems there is a problem with some path of the pyenv: I've tried export DJANGO_SETTINGS_MODULE=mrx_proj.settings but with no success.

The solution that I found is to configure the test runner the proper way:
The settings need to point to Django Settings: DJANGO_SETTINGS_MODULE = test_settings
pytest-django
Django Testing

Related

How to properly import an existing Django project in Eclipse/Pydev?

I cannot figure how to import correctly an existing Django project in Eclipse/Pydev, and I can't find my way in the documentation of Pydev. My question is pretty simple : I have an existing project from a different computer, that I want to migrate on a new one.
I installed Eclipse and Pydev on the new machine, and everything seems normal for a new project: I can create a new Django project and run it without issue. I also have been able to import my pre-existing project, but when I run the project, I encounter the following error :
Finding files... done.
Importing test modules ... Traceback (most recent call last):
File "/home/francois/.p2/pool/plugins/org.python.pydev.core_9.3.0.202203051235/pysrc/_pydev_runfiles/pydev_runfiles.py", line 468, in __get_module_from_str
mod = __import__(modname)
File "/home/francois/eclipse-workspace/Test/Test/urls.py", line 20, in <module>
path('', include('myApp.urls')),
File "/home/francois/anaconda3/envs/ipaidenv/lib/python3.10/site-packages/django/urls/conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "/home/francois/anaconda3/envs/ipaidenv/lib/python3.10/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/home/francois/eclipse-workspace/Test/myApp/urls.py", line 2, in <module>
from . import views
File "/home/francois/eclipse-workspace/Test/myApp/views.py", line 2, in <module>
from myApp.forms import CreateGroupForm, JoinGroupForm
File "/home/francois/eclipse-workspace/Test/myApp/forms.py", line 4, in <module>
from .models import Group, User
File "/home/francois/eclipse-workspace/Test/myApp/models.py", line 2, in <module>
from django.contrib.auth.models import AbstractUser
File "/home/francois/anaconda3/envs/ipaidenv/lib/python3.10/site-packages/django/contrib/auth/models.py", line 3, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/home/francois/anaconda3/envs/ipaidenv/lib/python3.10/site-packages/django/contrib/auth/base_user.py", line 48, in <module>
class AbstractBaseUser(models.Model):
File "/home/francois/anaconda3/envs/ipaidenv/lib/python3.10/site-packages/django/db/models/base.py", line 108, in __new__
app_config = apps.get_containing_app_config(module)
File "/home/francois/anaconda3/envs/ipaidenv/lib/python3.10/site-packages/django/apps/registry.py", line 253, in get_containing_app_config
self.check_apps_ready()
File "/home/francois/anaconda3/envs/ipaidenv/lib/python3.10/site-packages/django/apps/registry.py", line 136, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
ERROR: Module: Test.urls could not be imported (file: /home/francois/eclipse-workspace/Test/Test/urls.py).
done.
Traceback (most recent call last):
File "/home/francois/.p2/pool/plugins/org.python.pydev.core_9.3.0.202203051235/pysrc/runfiles.py", line 273, in <module>
main()
File "/home/francois/.p2/pool/plugins/org.python.pydev.core_9.3.0.202203051235/pysrc/runfiles.py", line 97, in main
return pydev_runfiles.main(configuration) # Note: still doesn't return a proper value.
File "/home/francois/.p2/pool/plugins/org.python.pydev.core_9.3.0.202203051235/pysrc/_pydev_runfiles/pydev_runfiles.py", line 874, in main
PydevTestRunner(configuration).run_tests()
File "/home/francois/.p2/pool/plugins/org.python.pydev.core_9.3.0.202203051235/pysrc/_pydev_runfiles/pydev_runfiles.py", line 801, in run_tests
get_django_test_suite_runner()(run_tests).run_tests([])
File "/home/francois/anaconda3/envs/ipaidenv/lib/python3.10/site-packages/django/test/runner.py", line 723, in run_tests
databases = self.get_databases(suite)
File "/home/francois/anaconda3/envs/ipaidenv/lib/python3.10/site-packages/django/test/runner.py", line 702, in get_databases
databases = self._get_databases(suite)
File "/home/francois/anaconda3/envs/ipaidenv/lib/python3.10/site-packages/django/test/runner.py", line 690, in _get_databases
for test in suite:
TypeError: 'NoneType' object is not iterable
ipaidenv is the name of my conda environment, Test is the project name.
I am pretty sure there is something very simple that I didn't do right, but I can't find what, can anyone help?
Many thanks!

Django import my app models AppRegistryNotReady

I will import my models in celery.py. But when I import and run the runserver command, I get the following error:
File "/directory/manage.py", line 22, in <module>
main()
File "/directory/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/directory/venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
utility.execute()
File "/directory/venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 363, in execute
settings.INSTALLED_APPS
File "/directory/venv/lib/python3.9/site-packages/django/conf/__init__.py", line 82, in __getattr__
self._setup(name)
File "/directory/venv/lib/python3.9/site-packages/django/conf/__init__.py", line 69, in _setup
self._wrapped = Settings(settings_module)
File "/directory/venv/lib/python3.9/site-packages/django/conf/__init__.py", line 170, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
......
File "/directory/__init__.py", line 1, in <module>
from .celery import app as celery_app
File "/directory/celery.py", line 9, in <module>
from apps.models import Model1
File "/directory/apps/models.py", line 2, in <module>
from django.contrib.auth.models import User, AbstractUser
File "/directory/venv/lib/python3.9/site-packages/django/contrib/auth/models.py", line 3, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/directory/venv/lib/python3.9/site-packages/django/contrib/auth/base_user.py", line 48, in <module>
class AbstractBaseUser(models.Model):
File "/directory/venv/lib/python3.9/site-packages/django/db/models/base.py", line 108, in __new__
app_config = apps.get_containing_app_config(module)
File "/directory/venv/lib/python3.9/site-packages/django/apps/registry.py", line 253, in get_containing_app_config
self.check_apps_ready()
File "/directory/venv/lib/python3.9/site-packages/django/apps/registry.py", line 136, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
My import code line in celery.py:
from app.models import model1, model2
You can try adding this line to your settings.py file:
import django
django.setup()
See here for solution.
if it doesn't work, this might be what you need:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project.settings")
and these lines:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
If the first solution doesn't work here the alternative solution.
Finally, there is such a solution:
import django
# some variable declarations
world_mapping = {
'osm_id': 'osm_id',
}
if __name__ == '__main__':
django.setup()
# import AFTER setup
from app.models import WorldBorder
# from now I can access WorldBorder!!
I solved it by copying all tasks from celery.py to app tasks.py.
It also fixes itself when I delete celery related lines from __init__.py file.

Problem with Django app unit tests under Visual Studio Code

I have a draft of the Django project with the added one application (e.g. my_app). In this app I have places tests.py file with one test:
import unittest
class Test_MyModel(unittest.TestCase):
def test_dummy(self):
self.assertEqual(1,1)
In this case, this dummy test is discoverd by Visual Studio Code and could be executed in it, also it's possible to launch this test from command line:
python manage.py test
When I modify my file with unit test add import some model (placed in file models.py) from my_app:
import unittest
from .models import MyModel # new added line
class Test_MyModel(unittest.TestCase):
def test_dummy(self):
self.assertEqual(1,1)
In this case I'm still be able run test from command line without any problems, but VSC doesn't discover my test and in the Python Test Log in the VSC I get error:
======================================================================
ERROR: tests (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: tests
Traceback (most recent call last):
File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/loader.py", line 434, in _find_test_path
module = self._get_module_from_name(name)
File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/loader.py", line 375, in _get_module_from_name
__import__(name)
File "/Users/myuser/Projects/backend/my_app/tests.py", line 3, in <module>
from my_app.models import MyModel
File "/Users/myuser/Projects/backend/my_app/models.py", line 4, in <module>
class MyModel(models.Model):
File "/Users/myuser/Projects/virtualenvs/my_app_env/lib/python3.7/site-packages/django/db/models/base.py", line 103, in __new__
app_config = apps.get_containing_app_config(module)
File "/Users/myuser/Projects/virtualenvs/my_app_env/lib/python3.7/site-packages/django/apps/registry.py", line 252, in get_containing_app_config
self.check_apps_ready()
File "/Users/myuser/Projects/virtualenvs/my_app_env/lib/python3.7/site-packages/django/apps/registry.py", line 134, in check_apps_ready
settings.INSTALLED_APPS
File "/Users/myuser/Projects/virtualenvs/my_app_env/lib/python3.7/site-packages/django/conf/__init__.py", line 79, in __getattr__
self._setup(name)
File "/Users/myuser/Projects/virtualenvs/my_app_env/lib/python3.7/site-packages/django/conf/__init__.py", line 64, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
Any hints or advice how can I resolve this problem and start fully using unit test under VSC for models test?
I've found a solution to my problem. I need to add below lines into my init.py file in my_app folder.
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_app_project.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Django AppRegistryNotReady when defining a model

I wanted to add the first model to an already working app, and now I can't start it because it always gives AppRegistryNotReady. This does only happen, if my model MailLog is child of models.Model.
from __future__ import unicode_literals
from django.db import models
#class MailLog(models.Model): # like this, it crashes
class MailLog(): # like this, it works
# Field definitions
recipient = models.EmailField()
created = models.DateTimeField(auto_now_add=True)
template = models.CharField(max_length=500)
error = models.TextField(null=True)
def __str__(self):
return self.recipient+" "+self.template+"("+str(self.created)+")"
The error occurs no matter what's inside the class, even if there is only a pass. However, I can import my models in the admin.py, and it crashes when I import from a file I called core.py. That file looks like this:
import boto3
from botocore.exceptions import ClientError
from django.template import loader
from django.conf import settings
from .templates import *
from .models import MailLog
The traceback looks like this
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/home/niels/anaconda3/envs/app/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/home/niels/anaconda3/envs/app/lib/python2.7/site-packages/django/core/management/__init__.py", line 316, in execute
settings.INSTALLED_APPS
File "/home/niels/anaconda3/envs/app/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
self._setup(name)
File "/home/niels/anaconda3/envs/app/lib/python2.7/site-packages/django/conf/__init__.py", line 41, in _setup
self._wrapped = Settings(settings_module)
File "/home/niels/anaconda3/envs/app/lib/python2.7/site-packages/django/conf/__init__.py", line 97, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/home/niels/anaconda3/envs/app/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/niels/Dokumente/jobapp/deploy/backend/settings.py", line 255, in <module>
django.setup()
File "/home/niels/anaconda3/envs/app/lib/python2.7/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/niels/anaconda3/envs/app/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/home/niels/anaconda3/envs/app/lib/python2.7/site-packages/django/apps/config.py", line 90, in create
module = import_module(entry)
File "/home/niels/anaconda3/envs/app/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/niels/Dokumente/jobapp/deploy/ses_mail/__init__.py", line 1, in <module>
from .core import send
File "/home/niels/Dokumente/jobapp/deploy/ses_mail/core.py", line 7, in <module>
from .admin import MailLog
File "/home/niels/Dokumente/jobapp/deploy/ses_mail/admin.py", line 2, in <module>
from .models import *
File "/home/niels/Dokumente/jobapp/deploy/ses_mail/models.py", line 6, in <module>
class MailLog(models.Model):
File "/home/niels/anaconda3/envs/app/lib/python2.7/site-packages/django/db/models/base.py", line 105, in __new__
app_config = apps.get_containing_app_config(module)
File "/home/niels/anaconda3/envs/app/lib/python2.7/site-packages/django/apps/registry.py", line 237, in get_containing_app_config
self.check_apps_ready()
File "/home/niels/anaconda3/envs/app/lib/python2.7/site-packages/django/apps/registry.py", line 124, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
You should not be importing Django models in your app's __init__ (or causing them to be imported by importing from core. If you use an empty __init__.py it should stop the error.

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