0

我正在尝试使用 Django 1.10、Python 3.5 单独运行脚本,并且代码将使用 Django 模型。

该脚本位于 Django 项目的根目录中。

import os
import sys
import django
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "proj.settings")
application = get_wsgi_application()


from pipeline.models import *
c = ModelName.objects.all()

我收到 django.core.exceptions.AppRegistryNotReady 的错误:尚未加载应用程序。对于以下代码行。应用程序 = get_wsgi_application()

之前我是使用django-extension-runscript来运行这些脚本来执行数据处理任务,但是它依赖于manage.py,它与celery不兼容(提供周期性任务管理。)

您有解决问题的想法或如何将 runscript 与 celery 结合使用吗?

这是完整的错误轨迹

Traceback (most recent call last):
  File "celery_test.py", line 25, in <module>
    application = get_wsgi_application()
  File "/home/ah/anaconda2/envs/zorro/lib/python3.5/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
    django.setup(set_prefix=False)
  File "/home/ah/anaconda2/envs/zorro/lib/python3.5/site-packages/django/__init__.py", line 22, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/home/ah/anaconda2/envs/zorro/lib/python3.5/site-packages/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "/home/ah/anaconda2/envs/zorro/lib/python3.5/site-packages/django/conf/__init__.py", line 41, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/ah/anaconda2/envs/zorro/lib/python3.5/site-packages/django/conf/__init__.py", line 97, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/home/ah/anaconda2/envs/zorro/lib/python3.5/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 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/ah/project/zorro/zorro/__init__.py", line 5, in <module>
    from .celery import app as celery_app
  File "/home/ah/project/zorro/zorro/celery.py", line 5, in <module>
    from pipeline.tasks import run_cmc
  File "/home/ah/project/zorro/pipeline/tasks.py", line 2, in <module>
    from pipeline.scripts.celery_test import celery_test
  File "/home/ah/project/zorro/pipeline/scripts/celery_test.py", line 1, in <module>
    from pipeline.models import CleanedJob
  File "/home/ah/project/zorro/pipeline/models/__init__.py", line 1, in <module>
    from .models_general import *
  File "/home/ah/project/zorro/pipeline/models/models_general.py", line 7, in <module>
    from pipeline.models.models_timestamp import TimeStamped
  File "/home/ah/project/zorro/pipeline/models/models_timestamp.py", line 5, in <module>
    class TimeStamped(models.Model):
  File "/home/ah/anaconda2/envs/zorro/lib/python3.5/site-packages/django/db/models/base.py", line 105, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/home/ah/anaconda2/envs/zorro/lib/python3.5/site-packages/django/apps/registry.py", line 237, in get_containing_app_config
    self.check_apps_ready()
  File "/home/ah/anaconda2/envs/zorro/lib/python3.5/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.

我通过删除 proj/proj 目录中与 Celery 相关的所有文件(包括 celery.py、tasks.py 和init .py 中的内容)和 app_name/tasks 解决了这个问题。

但它们如何协同工作?

4

0 回答 0