Search code examples
pythondjangoregistrytaskcelery

Celery Django not Registering Tasks, Module not found error in shell


I am trying to run a basic example from the Celery docs, but when I run 'from tasks import add' it gives me an error saying the 'module is not found'.

These are the files I have changed.

proj/proj/celery.py

from __future__ import absolute_import

import os

from celery import Celery

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

from django.conf import settings

app = Celery('proj')

# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)


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

proj/proj/settings.py

BROKER_URL = 'amqp://guest:guest@localhost//'

#: Only add pickle to this list if your broker is secured
#: from unwanted access (see userguide/security.html)
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'


INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'celerymod',
    'djcelery',
)

proj/proj/init.py

from __future__ import absolute_import
from .celery import app as celery_app

proj/celerymod/tasks.py

from __future__ import absolute_import

from celery import shared_task


@shared_task
def add(x, y):
    return x + y


@shared_task
def mul(x, y):
    return x * y


@shared_task
def xsum(numbers):
    return sum(numbers)

Appreciate any suggestions. Thank you!


Solution

  • If anyone else have the same issue. Do this in shell. myapp is the name of the application, in my case it is proj.

    from myapp.tasks import add