user2361174
user2361174

Reputation: 1942

"django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet." For startup code

So I'm trying to add some code to my Django 1.10 app that will only run when the server starts. I took the instructions on how to do this from here and here. Every time I try running the python console I get this:

C:\Users\username\Envs\projectName [master ≡ +0 ~2 -0 !]> python manage.py shell
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\management\__init__.py", line 341, in execute
    django.setup()
  File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\site-packages\django\__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\site-packages\django\apps\registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\site-packages\django\apps\config.py", line 90, in create
    module = import_module(entry)
  File "C:\Users\username\AppData\Local\Programs\Python\Python3\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 222, in_call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 662, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in_call_with_frames_removed
  File "C:\Users\username\Envs\projectName\appName\apps.py", line 2, in <module>
    import appName.osm as osm
  File "C:\Users\username\Envs\projectName\appName\osm.py", line 4, in <module>
    from .models import Country, Embassy
  File "C:\Users\username\Envs\projectName\appName\models.py", line 38, in <module>
    class Country(models.Model):
  File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\models\base.py", line 105, in __new__
    app_config = apps.get_containing_app_config(module)
  File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\site-packages\django\apps\registry.py", line 237, in get_containing_app_config
    self.check_apps_ready()
  File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\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.

I tried looking this up but it seems that everyone who gets this exception is because it relates to an update issue with Django, however I've been using 1.10 for a long time now (not sure if I ever touched anything older). Any idea what's going on?

projectName/appName/apps.py:

from django.apps import AppConfig
import finder.osm as osm # this seems to cause the issue

class FinderConfig(AppConfig):
    name = 'finder'
    init = True
    def ready(self):
        pass 

projectName/appName/__init__.py:

default_app_config = 'appName.apps.AppNameConfig'

projectName/appName/models.py:

from django.db import models

class Country(models.Model):
    #...

class Embassy(models.Model):
    #...

Upvotes: 1

Views: 589

Answers (1)

user2361174
user2361174

Reputation: 1942

As @Andrey Shipilov has commented, it was a case of bad imports as you cannot import on the same level as the AppConfig class. A fix to my solution was to move the import into AppConfig.ready()

projectName/appName/apps.py:

from django.apps import AppConfig


class FinderConfig(AppConfig):
    import finder.osm as osm 
    name = 'finder'
    init = True
    def ready(self):
        pass 

Upvotes: 1

Related Questions