Search code examples
djangoimporterrormanage.py

Cannot fix django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet


I run the following line of code on a Docker container:

RUN python3 manage.py sitetree_resync_apps --settings=sites.production.settings

and I get the following error:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 377, in execute
    django.setup()
  File "/usr/local/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python3.6/site-packages/django/apps/registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "/usr/local/lib/python3.6/site-packages/django/apps/config.py", line 116, in create
    mod = import_module(mod_path)
  File "/usr/lib64/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/xxx/xxx/invoicing/apps.py", line 10, in <module>
    from xxx.site.signals import resource_event, notify_by_email
  File "/xxx/xxx/site/signals.py", line 12, in <module>
    from xxx.backend.email import send_email
  File "/xxx/xxx/backend/email.py", line 11, in <module>
    from post_office import mail
  File "/usr/local/lib/python3.6/site-packages/post_office/mail.py", line 13, in <module>
    from .models import Email, EmailTemplate, Log, PRIORITY, STATUS
  File "/usr/local/lib/python3.6/site-packages/post_office/models.py", line 27, in <module>
    class Email(models.Model):
  File "/usr/local/lib/python3.6/site-packages/django/db/models/base.py", line 108, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/usr/local/lib/python3.6/site-packages/django/apps/registry.py", line 253, in get_containing_app_config
    self.check_apps_ready()
  File "/usr/local/lib/python3.6/site-packages/django/apps/registry.py", line 136, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

I have tried to update Django version and several other libraries. The error changes but once fixed in the end I always end up on the above error! Can someone help?

my requirement file looks like:

Django==3.1.3
Pillow==3.0.0
XlsxWriter==0.8.4
amqp==1.4.9
anyjson==0.3.3
backports.functools-lru-cache==1.6.1
beautifulsoup4==4.8.2
billiard==3.3.0.23
celery==3.1.26.post2
cssutils==1.0.2
django-autocomplete-light==2.1.1
django-braces==1.14.0
django-bootstrap3==6.2.2
django-bootstrap3-datetimepicker-2==2.4.2
django-celery==3.3.1
django-cors-headers==3.5.0
django-crispy-forms==1.5.2
django-datatable-view==0.8.2
django-extensions==2.0.7
django-formtools==1.0
django-grappelli==2.7.1
django-ical==1.3
django-hstore==1.4.2
django-inlinecss==0.1.2
django-model-utils==2.5.2
django-money==0.7.4
django-oauth-toolkit==1.0.0
djangorestframework==3.9.4
django-sortedm2m==2.0.0
django-sitetree==1.16.0
django-tag-parser==2.1
django-taggit==0.17.1
django-tastypie==0.12.2
django-timedeltafield==0.7.8
django-post-office==3.5.2
et-xmlfile==1.0.1
funcsigs==1.0.2
icalendar==4.0.4
jdcal==1.4.1
jsonfield==3.1.0
jsonpickle==1.4.1
kombu==3.0.37
matplotlib==3.2.2
mock==3.0.5
numpy==1.16.5
oauthlib==2.0.1
openpyxl==2.3.0
pandas==0.24.2
psycopg2==2.6
py-moneyed==0.6.0
pynliner3==0.6
python-dateutil==2.8.1
python-mimeparse==1.6.0
pytz==2019.3
six==1.10.0
soupsieve==1.9.5
uWSGI==2.0.18
unicodecsv==0.14.1
vine==1.3.0

I located the error on the manage.py file:

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xxx.settings")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

If I comment out the offending line in the Dockerfile the container successfully build but when I try to hop into it with the code:

 docker-compose up -d && docker-compose logs -f

I get the error:


nettle_1  | Traceback (most recent call last):
nettle_1  |   File "manage.py", line 8, in <module>
nettle_1  |     from django.core.management import execute_from_command_line
nettle_1  | ImportError: No module named django.core.management
exited with code 1

I am completely lost here.

EDITED Hi Melvin, this is my app.py

from mcvitty.site.signals import resource_event, notify_by_email
from django.apps import AppConfig

class InvoicingConfig(AppConfig):
    name = 'mcvitty.invoicing'
    verbose_name = "Invoices"

    def ready(self):
        resource_event.connect(notify_by_email, sender='invoicing.Invoice')

Due to my lack of experience with django I still fails to understand the error and how to set app.py correctly


Solution

  • Your problem is here:

      File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
      File "/xxx/xxx/invoicing/apps.py", line 10, in <module>
        from xxx.site.signals import resource_event, notify_by_email
      File "/xxx/xxx/site/signals.py", line 12, in <module>
        from xxx.backend.email import send_email
      File "/xxx/xxx/backend/email.py", line 11, in <module>
        from post_office import mail
    

    Your invoicing apps imports signals that import models from post_office. You should only register signals when all apps are loaded, through AppConfig.ready() as is explained in the documentation:

    Subclasses can override this method to perform initialization tasks such as registering signals. It is called as soon as the registry is fully populated.

    For clarity: this includes importing utilities / modules that import models from other apps as that lies at the core of the problem.