Python django:django.core.exceptions.AppRegistryNotReady:应用程序尚未加载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33186413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:58:41  来源:igfitidea点击:

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

pythondjango

提问by Peter Tsung

I was stuck with the process when I wanted to deploy django project on server today. When I run python manage.py runserveron server, the terminal shows me this:

当我今天想在服务器上部署 django 项目时,我被这个过程困住了。当我python manage.py runserver在服务器上运行时,终端向我显示:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 351, in execute_from_command_line
    utility.execute()
  File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 343, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 177, in fetch_command
    commands = get_commands()
  File "/usr/lib/python2.7/site-packages/django/utils/lru_cache.py", line 101, in wrapper
    result = user_function(*args, **kwds)
  File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 72, in get_commands
    for app_config in reversed(list(apps.get_app_configs())):
  File "/usr/lib/python2.7/site-packages/django/apps/registry.py", line 137, in get_app_configs
    self.check_apps_ready()
  File "/usr/lib/python2.7/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.

The django version on the server is 1.8.5, and the local is 1.8.1. I doubt the version may cause this problem. But I also doubted the wsgi.pywasn't written properly, here's the wsgi.py:

服务器上的django版本是1.8.5,本地是1.8.1。我怀疑版本可能会导致此问题。但我也怀疑wsgi.py没有写好,这里是wsgi.py

import os
import sys

path = '/Users/Peterhon/Desktop/dict/'
if path not in sys.path:
    sys.path.append(path)

os.chdir(path)

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dict.settings")

import django
django.setup()

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Here's the manage.pyfile:

这是manage.py文件:

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

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

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.arg)

When I run python manage.py check on server, the output is below:

当我在服务器上运行 python manage.py check 时,输出如下:

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

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

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

Could anyone give me some tips? Thanks too much

谁能给我一些提示?非常感谢

采纳答案by jbasko

This could well be an issue with your Django settings. For example, I just had specified in LOGGINGa filename in a non-existent directory. As soon as I changed it to an existing directory, the issue was resolved.

这很可能是您的 Django 设置的问题。例如,我刚刚LOGGING在一个不存在的目录中指定了一个文件名。一旦我将其更改为现有目录,问题就解决了。

回答by jamesc

./manage.py runserverwill use your wsgi.pyhowever it looks like the stack trace you've shown at the top does not include the wsgi file. Therefore the error is occurring before the wsgi file is loaded.

./manage.py runserver将使用您的wsgi.py但是看起来您在顶部显示的堆栈跟踪不包含 wsgi 文件。因此错误发生在 wsgi 文件加载之前。

I'm not able to recreate your error, but since you seem to be using the new style wsgi and as you mention "the django version on the server is 1.8.5, and the local is 1.8.1", I'm wondering if there might be something wrong in your environment.

我无法重现您的错误,但是由于您似乎正在使用新样式 wsgi 并且正如您提到的“服务器上的 django 版本是 1.8.5,而本地版本是 1.8.1”,我想知道如果您的环境可能有问题。

My suggested steps are:

我建议的步骤是:

  1. Rebuild your virtualenv. Delete the env folder and install again with pip install -r requirements.txtor similar.
  2. Check out other questions on StackOverflow - this seems to be a common issue. E.g as above: Django 1.7 throws django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet
  1. 重建你的 virtualenv。删除 env 文件夹并重新安装pip install -r requirements.txt或类似。
  2. 查看有关 StackOverflow 的其他问题 - 这似乎是一个常见问题。例如如上:Django 1.7 throws django.core.exceptions.AppRegistryNotReady: 模型尚未加载

Hopefully someone with more experience will be able to add suggestions. Sorry I don't have the definitive answer for you.

希望有更多经验的人能够添加建议。抱歉,我没有给你明确的答案。

回答by infosmith

I ran into this issue today. 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.

我今天遇到了这个问题。INSTALLED_APPS 中有一个不存在的应用程序。删除后,它解决了异常。因任何原因无法导入的应用程序也会引发 AppRegistryNotReady 异常。

Here's the bug history for this issue.

这是此问题的错误历史记录。

回答by Waykos

There can be issue in your settings.py file . Specifically INSTALLED_APPS verify if you correctly included apps and separated with "," .

您的 settings.py 文件中可能存在问题。特别是 INSTALLED_APPS 验证您是否正确包含应用程序并用“,”分隔。

回答by Sarit

I got this error too regardless of the existing answers. I solve by do locally import in the celery function.

无论现有答案如何,我也收到此错误。我通过在 celery 函数中进行本地导入来解决。

回答by tstoev

I fixed it by setting the owner of the wsgi entry point that the apache calls to root:root

我通过将 apache 调用的 wsgi 入口点的所有者设置为 root:root 来修复它