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.
Here is my directory structure:
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))
Command: celery -A myapp worker -l info
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']
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")
Edit.
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
Your celery.py file should be in myapp folder. (http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html)
You should run celery -A myapp worker -l info in mysite folder.