将Django项目从Python 2转换为Python 3:django.core.exceptions.AppRegistryNotReady:应用尚未加载

时间:2019-10-28 20:59:45

标签: python django python-3.x python-2.7 python-2to3

我正在将Django网站从Python 2转换为Python3。为此,我在整个项目上运行了2to3。现在,在运行服务器时(在Python 2中运行良好)。现在,似乎有一个错误django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.,它似乎是由加载django_comments应用程序引起的。我不清楚这是否与模板/模型问题有关。在此项目的虚拟环境中安装了Python3。

(env) user:project user$ python3 manage.py check
Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_from_command_line(sys.argv)
  File ".../src/project/env/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File ".../src/project/env/lib/python3.7/site-packages/django/core/management/__init__.py", line 357, in execute
    django.setup()
  File ".../src/project/env/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File ".../src/project/env/lib/python3.7/site-packages/django/apps/registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File ".../src/project/env/lib/python3.7/site-packages/django/apps/config.py", line 90, in create
    module = import_module(entry)
  File ".../src/project/env/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File ".../src/project/language/__init__.py", line 3, in <module>
    from django_comments.models import Comment
  File ".../src/project/env/lib/python3.7/site-packages/django_comments/models.py", line 7, in <module>
    from .abstracts import (
  File ".../src/project/env/lib/python3.7/site-packages/django_comments/abstracts.py", line 4, in <module>
    from django.contrib.contenttypes.fields import GenericForeignKey
  File ".../src/project/env/lib/python3.7/site-packages/django/contrib/contenttypes/fields.py", line 3, in <module>
    from django.contrib.contenttypes.models import ContentType
  File ".../src/project/env/lib/python3.7/site-packages/django/contrib/contenttypes/models.py", line 133, in <module>
    class ContentType(models.Model):
  File ".../src/project/env/lib/python3.7/site-packages/django/db/models/base.py", line 103, in __new__
    app_config = apps.get_containing_app_config(module)
  File ".../src/project/env/lib/python3.7/site-packages/django/apps/registry.py", line 252, in get_containing_app_config
    self.check_apps_ready()
  File ".../src/project/env/lib/python3.7/site-packages/django/apps/registry.py", line 135, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

我还确保在manage.py中配置了DJANGO_SETTINGS_MODULE

  1 #!/usr/bin/env python3
  2
  3 import os
  4 import sys
  5
  6 if __name__ == "__main__":
  7     os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
  8
  9     from django.core.management import execute_from_command_line
 10
 11     execute_from_command_line(sys.argv)

2 个答案:

答案 0 :(得分:0)

也许您可以在manage.py中这样做:

import os
import django
import sys

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mis.settings")
django.setup()

from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)

答案 1 :(得分:0)

我刚刚找到了一个答案 pythonanywhere ,这意味着您不需要调用django.setup()。此外,您可以尝试在INSTALLED_APPS中逐个禁用您的应用程序,以便可以确定问题的出处。别忘了首先恢复manage.py中的代码!