Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django (& Pinax): Tracing back "AppRegistryNotReady: Apps aren't loaded yet." exception

I'm using Django 1.7.5, and trying to build the pinax-project-teams starter. When running python manage.py check, I get hit with `django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.'.

I've read through umpteen similar errors here on StackOverflow and elsewhere, and it seems this error comes up from a variety of causes and tends to involve pretty circumstance-specific fixes. So I can remove a couple solutions upfront: 1) This is not an upgrade from previous Django version; and 2) as a result, wsgi.py is correctly using the newer from django.core.wsgi import get_wsgi_application.

Full stacktrace:

(env)trevor@nikola:webapp.git$ python manage.py check
Traceback (most recent call last):
  File "manage.py", line 9, in <module>
    startup.run()
  File "/Users/trevor/zenith/webapp.git/djangoapp/startup.py", line 22, in run
    admin.autodiscover()
  File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/__init__.py", line 23, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "/usr/local/lib/python2.7/site-packages/django/utils/module_loading.py", line 67, in autodiscover_modules
for app_config in apps.get_app_configs():
  File "/usr/local/lib/python2.7/site-packages/django/apps/registry.py", line 137, in get_app_configs
    self.check_apps_ready()
  File "/usr/local/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.

The error stems from pinax's startup.run(), which consists of two calls: autoload(["receivers"]) which loads modules in settings.INSTALLED_APPS, and admin.autodiscover(). Here's autoload:

def autoload(submodules):
    for app in settings.INSTALLED_APPS:
        mod = import_module(app)
        # print('Module: \t\n%s' % str(mod))
        for submodule in submodules:
            # print('Submodule: \t\n%s\n' % str(submodule))
            try:
                import_module("{0}.{1}".format(app, submodule))
            except:
                if module_has_submodule(mod, submodule):
                    raise

This goes through INSTALLED_APPS and successfully imports all apps listed, but then right after admin.autodiscover() throws the Apps aren't loaded yet. message. I'm not sure what I'm missing... something obvious, likely!

Oh, and finally: I've read about the import django; django.setup() fix, but I thought that's for scripts & interactive, not django projects? Because I've tried running interactive, import my project settings, run configure(), run django.setup(), and exit, but that has no impact on the above issue when I try to execute runserver... :-/ Any ideas?

like image 798
TCAllen07 Avatar asked Mar 02 '15 22:03

TCAllen07


People also ask

What is the Django used for?

Django is a high-level Python web framework that enables rapid development of secure and maintainable websites. Built by experienced developers, Django takes care of much of the hassle of web development, so you can focus on writing your app without needing to reinvent the wheel.

Is Django same as Python?

Differences Between Django and Python Python has a set of language and object-oriented approach that helps programmers create clear and logical code. Django is a web development python framework that makes it easy to build powerful web applications.

Is Django backend or frontend?

Among the major benefits of using Django for back-end web development is its Representational State Transfer (REST) framework, which is a popular toolkit for building APIs. The power of Django's REST framework can be assessed from the fact that it takes just three lines of code to build a ready-to-use API.

Why Django is so popular?

According to GitHub, Django is the 2nd most starred Server Side framework after Laravel. The thing that makes Django a popular framework is its ability to strike the right balance between enterprise features and rapid application development. Moreover, the clean design of Django drags all developers.


1 Answers

Edit - Update & Summary: The pinax-project-teams code is not completely compatible with Django 1.7, at least not at the initial "raw" installation of a new Django project. A workaround consists of a few steps:

  1. Remove explicit/redundant app loading from startup.py (line 21, in run()): comment-out or delete admin.autodiscover().
  2. Explicitly install Django 1.6.5 (pip install Django==1.6.5).
  3. Sync database via django 1.6 version (python manage.py syncdb).
  4. Explicity install Django 1.7.5 (pip install Django==1.7.5).

Original Answer Post: I think I've narrowed it down a bit, if not in scope, at least in root cause. Though my project wasn't an upgrade from Django <=1.6 to 1.7, this is nonetheless an issue. Something in pinax-project-team makes it by default incompatible with Django 1.7. An identical install will allow syncdb and then runserver on a 1.6.5 django installation, but django-1.7.5 throws the Apps aren't loaded yet.

I found a nice long list of Django 1.7 release notes, which I'm starting through to see if I can determine the cause of the incompatibility.

At least one of the problems is Pinax's "extra" autodiscover in startup.py, which as of 1.7 release is automatically executed during Django's own startup process. This solves the app-loading issue, it appears... but there's another issue in django.contrib.sites. When running migrate in 1.7, one of the migrations fails due to a missing table in the DB, specifically django_site (full trace below). This table does exist in the 1.6 django install's db. Modifying the 1.7's settings to point to the sqlite file in the 1.6 project solves this problem, and voilà, it works. So those are the only two conflicts I've found between pinax-project-teams and Django 1.7.

(env)trevor@nikola:mysite7$ python manage.py migrate
Operations to perform:
  Synchronize unmigrated apps: wiki, account, eventlog, kaleo, profiles, easy_thumbnails, pinax_theme_bootstrap, teams, bootstrapform
  Apply all migrations: admin, contenttypes, sites, auth, sessions
Synchronizing apps without migrations:
  Creating tables...
  Installing custom SQL...
  Installing indexes...
Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 128, in handle
    created_models = self.sync_apps(connection, executor.loader.unmigrated_apps)
  File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 298, in sync_apps
    call_command('loaddata', 'initial_data', verbosity=self.verbosity, database=connection.alias, skip_validation=True, app_label=app_label, hide_empty=True)
  File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 115, in call_command
    return klass.execute(*args, **defaults)
  File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 61, in handle
    self.loaddata(fixture_labels)
  File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 91, in loaddata
    self.load_label(fixture_label)
  File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 148, in load_label
    obj.save(using=self.using)
  File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/core/serializers/base.py", line 173, in save
    models.Model.save_base(self.object, using=using, raw=True)
  File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/db/models/base.py", line 617, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/db/models/base.py", line 679, in _save_table
    forced_update)
  File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/db/models/base.py", line 723, in _do_update
    return filtered._update(values) > 0
  File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/db/models/query.py", line 600, in _update
    return query.get_compiler(self.db).execute_sql(CURSOR)
  File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1004, in execute_sql
    cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
  File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 786, in execute_sql
    cursor.execute(sql, params)
  File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/Users/trevor/code/pinax/7-pinaxtest/env/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 485, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: Problem installing fixture '/Users/trevor/code/pinax/7-pinaxtest/mysite7/fixtures/initial_data.json': Could not load sites.Site(pk=1): no such table: django_site
like image 100
TCAllen07 Avatar answered Sep 24 '22 13:09

TCAllen07