Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: AppRegistryNotReady()

Python: 2.7; Django: 1.7; Mac 10.9.4

I'm following the tutorial of Tango with Django

At Chapter 5, the tutorial teaches how to create a population script, which can automatically create some data for the database for the ease of development.

I created a populate_rango.py at the same level of manage.py.

Here's the populate_rango.py:

import os  def populate():     python_cat = add_cat('Python')      add_page(         cat=python_cat,         title="Official Python Tutorial",         url="http://docs.python.org/2/tutorial/"     )      add_page(         cat=python_cat,         title="How to Think like a Computer Scientist",         url="http://www.greenteapress.com/thinkpython/"     )      add_page(         cat=python_cat,         title="Learn Python in 10 Minutes",         url="http://www.korokithakis.net/tutorials/python/"     )      django_cat = add_cat("Django")      add_page(         cat=django_cat,         title="Official Django Tutorial",         url="https://docs.djangoproject.com/en/1.5/intro/tutorial01/"     )      add_page(         cat=django_cat,         title="Django Rocks",         url="http://www.djangorocks.com/"     )      add_page(         cat=django_cat,         title="How to Tango with Django",         url="http://www.tangowithdjango.com/"     )      frame_cat = add_cat("Other Frameworks")      add_page(         cat=frame_cat,         title="Bottle",         url="http://bottlepy.org/docs/dev/"     )      add_page(         cat=frame_cat,         title="Flask",         url="http://flask.pocoo.org"     )      for c in Category.objects.all():         for p in Page.objects.filter(category=c):             print "- {0} - {1}".format(str(c), str(p))   def add_page(cat, title, url, views=0):     p = Page.objects.get_or_create(category=cat, title=title, url=url, views=views)[0]     return p   def add_cat(name):     c = Category.objects.get_or_create(name=name)[0]     return c  if __name__ == '__main__':     print "Starting Rango population script..."     os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tangle.settings')     from rango.models import Category, Page     populate() 

Then I run python populate_rango.py at the terminal at the level of manage.py, AppRegistryNotReady() is raised:

django.core.exceptions.AppRegistryNotReady 

Then I googled it, found something like this:

Standalone scripts¶ If you’re using Django in a plain Python script — rather than a management command — and you rely on the DJANGO_SETTINGS_MODULE environment variable, you must now explicitly initialize Django at the beginning of your script with:  >>> import django >>> django.setup() Otherwise, you will hit an AppRegistryNotReady exception. 

And I still have no idea what should I do, can some one help? Thx!!!

like image 773
user2988464 Avatar asked Jul 17 '14 01:07

user2988464


People also ask

Why does Django throw an appregistrynotready exception when importing a dictionary?

Importing the dictionary from the app into the project caused Django to throw an AppRegistryNotReady exception. Likewise, importing items between apps can cause the exception.

How to configure Django to setup registry?

If you are using your django project applications in standalone scripts, in other words, without using manage.py- you need to manually call django.setup()first - it would configure the logging and, what is important - populate apps registry. Quote from Initialization processdocs: setup() This function is called automatically:

How to initialize Django in a plain Python script?

Standalone scripts¶ If you’re using Django in a plain Python script — rather than a management command — and you rely on the DJANGO_SETTINGS_MODULE environment variable, you must now explicitly initialize Django at the beginning of your script with: >>> import django >>> django.setup() Otherwise, you will hit an AppRegistryNotReady exception.

Why am I getting an appregistrynotready error when importing apps?

There was an app in INSTALLED_APPS that didn't exist. Once it was removed, it resolved the exception. Apps that can't be imported for any reason will also raise an AppRegistryNotReady exception. Here's the bug history for this issue.


1 Answers

If you are using your django project applications in standalone scripts, in other words, without using manage.py - you need to manually call django.setup() first - it would configure the logging and, what is important - populate apps registry.

Quote from Initialization process docs:

setup()

This function is called automatically:

  • When running an HTTP server via Django’s WSGI support.

  • When invoking a management command.

It must be called explicitly in other cases, for instance in plain Python scripts.

In your case, you need to call setup() manually:

if __name__ == '__main__':     print "Starting Rango population script..."     os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tangle.settings')      import django     django.setup()      populate() 

Also, this problem is described in detail in Troubleshooting section.

like image 51
alecxe Avatar answered Sep 25 '22 13:09

alecxe