【问题标题】:Django: Celery can't find my appDjango:Celery 找不到我的应用程序
【发布时间】:2017-09-25 18:11:49
【问题描述】:
ModuleNotFoundError: No module named 'myapp'

我真的不明白这里的问题是什么。我的应用程序没有问题,但无论我尝试和更改什么,celery 似乎都找不到它。

这是我的目录结构:

django
 / mysite
     __init__.py
     celery.py
     settings.py
     urls.py
     wsgi.py
     / myapp
       admin.py
       apps.py
       models.py
       tasks.py
       urls.py
       views.py
   manage.py

celery.py:

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')

app = Celery('myapp')

# Using a string here means the worker don't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()


@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

命令:celery -A myapp worker -l info

【问题讨论】:

  • 第一个命令结果:AttributeError: module 'celery' has no attribute 'celery' 第二个命令结果:ModuleNotFoundError: No module named 'myapp'
  • 我已经对我的答案添加了一个编辑。

标签: django django-models django-templates django-celery


【解决方案1】:

确保您的 init.py 文件包含以下内容:

from __future__ import absolute_import, unicode_literals

# This will make sure the app is always imported when
# Django starts so that shared_task will use this app. from .celery_worker import app as celery_app

__all__ = ['celery_app']

另外,请确保您的 celery.py 看起来与此类似

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
from celery.schedules import crontab
import mysite

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')

app = Celery('wellfie')

# Using a string here means the worker don't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()


@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

确保您的任务也被正确识别: 例如,共享任务看起来像

@shared_task(name="mysite.myapp.tasks.simple_task") 编辑。

实际上,您需要将 myapp 移出 mysite 并进入 Django 文件夹。我也会将 Django 文件夹重命名为 mysite,这样您就有了父文件夹 mysite、子文件夹 mysite,其中包含 wsgi、芹菜、设置等,子文件夹 myapp

【讨论】:

  • 我的代码看起来像这样,但我仍然遇到同样的错误。
  • @dcolumbus 实际上您需要将 myapp 移出 mysite 并进入 Django 文件夹。我也会将 Django 文件夹重命名为 mysite,这样您就有了父文件夹 mysite、子文件夹 mysite,其中包含 wsgi、芹菜、设置等,子文件夹 myall
  • [2017-05-08 21:53:56,629: ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 61] Connection refused. Trying again in 4.00 seconds...
  • 现在是一个愚蠢的问题 - 但是您是否安装并运行了 ampq? @dcolumbus
【解决方案2】:

您的celery.py 文件应位于myapp 文件夹中。 (http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html)

您应该在mysite 文件夹中运行 celery -A myapp worker -l info。

【讨论】:

  • [2017-05-08 21:53:56,629: ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 61] Connection refused. Trying again in 4.00 seconds...
  • 你还没有运行你的 rabbitmq 代理。
  • 解决办法是什么?
  • 您提到的链接谈到将 celery.py 保留在 proj/proj/celery.py 中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-05
  • 1970-01-01
  • 1970-01-01
  • 2023-02-25
  • 2013-01-04
  • 2023-04-11
  • 1970-01-01
相关资源
最近更新 更多