I'm trying to get periodic tasks running with my Django project as per this tutorial.
However, when trying to run the Celery worker with the command below:
celery -A myproject worker -l info
I get the following error:
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named celery
I suspect it may be because my settings files (local.py and production.py) are not as per the standard Django file structure, they are nested one folder lower.
├── myproject
│ ├── myproject
│ │ ├── __init__.py
│ │ ├── settings
│ │ │ ├── __init__.py
│ │ │ ├── base.py
│ │ │ ├── celery.py
│ │ │ ├── local.py
│ │ │ ├── production.py
│ ├── manage.py
__init__.py
# myproject/myproject/settings/__init__.py
from __future__ import absolute_import, unicode_literals
from .base import *
try:
from .local import *
live = False
except:
live = True
if live:
from .production import *
from .celery import app as celery_app
celery.py
# myproject/myproject/settings/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', 'myproject.settings.local')
app = Celery('myproject')
# 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))
from celery.schedules import crontab
app.conf.beat_schedule = {
'add-every-minute-contrab': {
'task': 'fetch_news',
'schedule': crontab()
}
}
Celery is definitely installed.
I've looked at a lot of other threads about this error and none of them have helped. Any advice would be much appreciated.
Thanks
Managed to fix by adding the following to __init__.py
in the directory above.
# myproject/myproject/__init__.py
from __future__ import absolute_import
from myproject.settings.celery import app as celery_app