Django:芹菜找不到我的应用程序

时间:2017-04-27 19:28:20

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

ModuleNotFoundError: No module named 'myapp'

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

这是我的目录结构:

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

2 个答案:

答案 0 :(得分:0)

确保 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,celery,设置等,子文件夹myapp

答案 1 :(得分:0)

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

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