Django and Celery - ModuleNotFoundError: No module named 'celery.task'

abdullah celik picture abdullah celik · Oct 22, 2020 · Viewed 9.1k times · Source

I wanted to start a periodic task in Django using Celery. Here are the relevant parts of my project:

# celery.py

from celery import Celery

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

app = Celery('bookProjectSetting')

# Using a string here means the worker doesn'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(f'Request: {self.request!r}')

The __init__.py looks like this:

# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app

__all__ = ('celery_app',)

My tasks.py file in my books app folder looks like this:

from books.models import Book
from celery.schedules import crontab
from celery.decorators import periodic_task

@periodic_task(
    run_every=(crontab(minute='*/10')), 
    name="update_life_time_of_books", 
    ignore_result=True)
def update_life_time_of_books():
   # do sth. 

I also set up redis as broker and installed the django-celery-beat stuff. But when I run the worker via the command:

celery -A bookProjectSetting beat -l INFO --scheduler django_celery_beat.schedulers:DatabaseScheduler

Then I get the following error:

File "/home/ac3l1k/Desktop/PROJECT/simpleDjangoProjects/bookProject/env/lib/python3.8/site-packages/celery/local.py", line 403, in _compat_periodic_task_decorator
    from celery.task import periodic_task
ModuleNotFoundError: No module named 'celery.task'

From the error I assume that something is wrong with the decorator but I can not tell why. Hope someone can help.

Thnks in advance :=)

NOTE: Here is the content of my requirements.txt file for version references

$ pip freeze

amqp==5.0.1
asgiref==3.2.10
billiard==3.6.3.0
celery==5.0.1
click==7.1.2
click-didyoumean==0.0.3
click-repl==0.1.6
Django==3.1.2
django-celery-beat==2.1.0
django-timezone-field==4.0
djangorestframework==3.12.1
kombu==5.0.2
prompt-toolkit==3.0.8
python-crontab==2.5.1
python-dateutil==2.8.1
pytz==2020.1
redis==3.5.3
six==1.15.0
sqlparse==0.4.1
vine==5.0.0
wcwidth==0.2.5

Answer

Chính Vương picture Chính Vương · Mar 10, 2021

The "task" is no longer in version 5.x You can use version 4.x

  • pip3 uninstall celery
  • pip3 install celery==4.4.2

Edited the spaces