问题描述
ModuleNotFoundError: No module named 'myapp'
我不是很了解这里的问题.我的应用程序没有问题,但是无论我尝试更改什么,芹菜似乎都找不到.
I'm not really understanding what the problem is here. There are no issues with my app, but celery can't seem to find it no matter what I try and change.
这是我的目录结构:
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:
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 -myapp worker -l信息
推荐答案
请确保您的 init .py文件包含以下内容:
Make sure that your init.py file contains the following:
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看起来与此类似
Also, make sure that your celery.py looks something along the lines of this
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))
确保您的任务也被正确识别:例如,共享任务看起来像
Make sure that your tasks are properly recognized as well:for instance a shared task would look like
@shared_task(name ="mysite.myapp.tasks.simple_task")
编辑.
实际上,您需要将myapp移出mysite并移至Django文件夹.我也将Django文件夹重命名为mysite,以便您拥有父文件夹mysite,子文件夹mysite(其中包含wsgi,celery,设置等),子文件夹myapp
Actually you need to move the myapp out of mysite and into the Django folder. I would rename the Django folder to mysite as well, so that you have the parent folder mysite, child folder mysite, that holds wsgi, celery, settings etc, child folder myapp
这篇关于Django:Celery找不到我的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!