无法修复 django.core.exceptions.AppRegistryNotReady:应用尚未加载

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

我 运行 在 Docker 容器上的以下代码行:

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

我收到以下错误:

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.

我尝试更新 Django 版本和其他几个库。错误发生了变化,但一旦最终修复,我总是以上述错误告终!有人可以帮忙吗?

我的需求文件如下所示:

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

我在 manage.py 文件中找到错误:

#!/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)

如果我在 Docker 文件中注释掉有问题的行,容器会成功构建,但是当我尝试使用代码跳入其中时:

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

我收到错误:


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

我完全迷失在这里。

已编辑 嗨,梅尔文, 这是我的 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')

由于我缺乏使用 django 的经验,我仍然无法理解错误以及如何正确设置 app.py

您的问题在这里:

  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

您的发票应用程序导入从 post_office 导入模型的信号。你应该只在所有应用程序加载时通过 AppConfig.ready() 注册信号,如 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.

为清楚起见:这包括导入从其他应用程序导入模型的实用程序/模块,因为这是问题的核心。