RuntimeError: App registry isn't ready yet

The upcoming Django 1.7 has many interesting and welcomed changes. One of them is an update to how applications are loaded.

The relase notes also mention potential problems with these changes. I ran into RuntimeError: App registry isn't ready yet while using the self-contained tests approach, but the fix is as simple as mentioned in the relase notes.

import django
django.setup()

If your code needs to run on Django < 1.7 as well you could use

# Django 1.7
try:
    import django
    django.setup()
except AttributeError:
    pass

Update: Dieter's comment actually made me think about my code for a second and I'll be using something different:

import django
if hasattr(django, 'setup'):
    django.setup()

Published on April 13, 2014 at 12:28 p.m. by Nicolas and tagged testing, Django. You can follow the discussion with the comment feed for this post. Feeling generous? Donate!

4 comments

  1. avatar
    wrote this comment on
    Well, that's the documented way to handle "it" (without the try, though). However, I had the problem that all test run ok when running through manage.py, but failed when started from PyCharm (all was well after I reverted to 1.6).

    I could not find the right place to put this code in that IDE . Reported it to JetBrains, got no solution.
    Reply to this comment
    1. avatar
      wrote this comment on
      I use the try because the code I was working on supports Django 1.4 to 1.7. I'll update my post, thanks! I hope PyCharm will be updated, but I wouldn't know, I'm a happy vim user ;-)
      Reply to this comment
  2. avatar
    wrote this comment on
    Hi Nicolas,
    Since you're targeting a known version of Django, you might want to be explicit about it, like:

    import django
    if django.VERSION >= (1, 7):
    django.setup()
    Reply to this comment
    1. avatar
      wrote this comment on
      Hi zyegfryed, thanks for your suggestion!
      Reply to this comment

Start a new thread

Cancel reply
Markdown. Syntax highlighting with <code lang="php"><?php echo "Hello, world!"; ?></code> etc.