Move to Django 1.7 - AppRegistryNotReady exception
I am struggling to get everything working after upgrading my Django version from 1.6.7 to 1.7. I can't seem to focus on the correct question. I am trying to resume the situation so far.
Thing: If I leave the django.setup () command in my wsgi.py file when I try to access my website, I get an internal server error (500). Looking at the logs I get:
[Sun Oct 12 12:38:50 2014] [error] [client 127.0.0.1] mod_wsgi (pid=23258): Target WSGI script '/home/thrasher/webapps/django/myproject.wsgi' cannot be loaded as Python module.
[Sun Oct 12 12:38:50 2014] [error] [client 127.0.0.1] mod_wsgi (pid=23258): Exception occurred processing WSGI script '/home/thrasher/webapps/django/myproject.wsgi'.
[Sun Oct 12 12:38:50 2014] [error] [client 127.0.0.1] Traceback (most recent call last):
[Sun Oct 12 12:38:50 2014] [error] [client 127.0.0.1] File "/home/thrasher/webapps/django/myproject.wsgi", line 16, in <module>
[Sun Oct 12 12:38:50 2014] [error] [client 127.0.0.1] application = get_wsgi_application()
[Sun Oct 12 12:38:50 2014] [error] [client 127.0.0.1] File "/home/thrasher/webapps/django/lib/python2.7/django/core/wsgi.py", line 14, in get_wsgi_application
[Sun Oct 12 12:38:50 2014] [error] [client 127.0.0.1] django.setup()
[Sun Oct 12 12:38:50 2014] [error] [client 127.0.0.1] File "/home/thrasher/webapps/django/lib/python2.7/django/__init__.py", line 21, in setup
[Sun Oct 12 12:38:50 2014] [error] [client 127.0.0.1] apps.populate(settings.INSTALLED_APPS)
[Sun Oct 12 12:38:50 2014] [error] [client 127.0.0.1] File "/home/thrasher/webapps/django/lib/python2.7/django/apps/registry.py", line 78, in populate
[Sun Oct 12 12:38:50 2014] [error] [client 127.0.0.1] raise RuntimeError("populate() isn't reentrant")
[Sun Oct 12 12:38:50 2014] [error] [client 127.0.0.1] RuntimeError: populate() isn't reentrant
However, if I comment out the django.setup () call, trying to access the website gets me this stack trace:
Environment:
Request Method: GET
Request URL: http://www.creepyvisions.it/
Django Version: 1.7
Python Version: 2.7.8
Installed Applications:
('django.contrib.auth',
'django.contrib.admin.apps.SimpleAdminConfig',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.admindocs',
'myproject.archivio',
'sorl.thumbnail',
'django.contrib.sitemaps',
'rest_framework')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "/home/thrasher/webapps/django/lib/python2.7/django/core/handlers/base.py" in get_response
98. resolver_match = resolver.resolve(request.path_info)
File "/home/thrasher/webapps/django/lib/python2.7/django/core/urlresolvers.py" in resolve
338. for pattern in self.url_patterns:
File "/home/thrasher/webapps/django/lib/python2.7/django/core/urlresolvers.py" in url_patterns
367. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/thrasher/webapps/django/lib/python2.7/django/core/urlresolvers.py" in urlconf_module
361. self._urlconf_module = import_module(self.urlconf_name)
File "/usr/local/lib/python2.7/importlib/__init__.py" in import_module
37. __import__(name)
File "/home/thrasher/webapps/django/myproject/urls.py" in <module>
33. url(r'^admin/', include(admin.site.urls)),
File "/home/thrasher/webapps/django/lib/python2.7/django/contrib/admin/sites.py" in urls
260. return self.get_urls(), self.app_name, self.name
File "/home/thrasher/webapps/django/lib/python2.7/django/contrib/admin/sites.py" in get_urls
221. self.check_dependencies()
File "/home/thrasher/webapps/django/lib/python2.7/django/contrib/admin/sites.py" in check_dependencies
159. if not apps.is_installed('django.contrib.admin'):
File "/home/thrasher/webapps/django/lib/python2.7/django/apps/registry.py" in is_installed
223. self.check_apps_ready()
File "/home/thrasher/webapps/django/lib/python2.7/django/apps/registry.py" in check_apps_ready
124. raise AppRegistryNotReady("Apps aren't loaded yet.")
Exception Type: AppRegistryNotReady at /
Exception Value: Apps aren't loaded yet.
To complete this, this is the wsgi related code:
myprojext.wsgi
import os
import sys
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
wgsi.py
import django
from django.core.handlers.wsgi import WSGIHandler
def get_wsgi_application():
#django.setup()
return WSGIHandler()
Everything looks very strange to me, I've done a lot of searches in the official Django docs and various forums, but I still can't get it to work correctly. Any suggestions would be much appreciated.
source to share
It turned out that the problem lies in the duplicate django.contrib.admin file in INSTALLED_APPS. It looks like this was the root of the problem. As soon as I removed the second link, I dug django.setup () into wsgi.py and everything became sunny and clear again. Everything is working fine now.
source to share