Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 944
abdullah celik Avatar asked Oct 22 '20 13:10

abdullah celik


People also ask

What is Django celery in Python?

django-celery provides Celery integration for Django; Using the Django ORM and cache backend for storing results, autodiscovery of task modules for applications listed in INSTALLED_APPS, and more.

How to store celery task results using the Django ORM?

This extension enables you to store Celery task results using the Django ORM. It defines a single model ( django_celery_results.models.TaskResult ) used to store task results, and you can query this database table like any other Django model. The installation instructions for this extension is available from the Celery documentation _

Where is my celery bin file located?

My bin file for celery is placed in /home/user/.local/bin/celery. Directly running celery -A project worker -l info in terminal is working fine. @Azat Ibrakov I tried adding a different name for the celery.py named previously to celery_app.py, still no hope. @Joe A, from celery import Celery import won't cause any issue here.

How to install Django-celery-results in Python?

You can install django-celery-results either via the Python Package Index (PyPI) or from source. To install using pip ,:: .. _installing-from-source:


2 Answers

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

like image 194
Chính Vương Avatar answered Sep 18 '22 08:09

Chính Vương


They deprecated that functionality as I understood from this issue: https://github.com/celery/celery/issues/6406

Now it seems to be that you need to use this to make scheduled tasks:

https://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html?highlight=periodic

like image 33
Kaan Goksal Avatar answered Sep 19 '22 08:09

Kaan Goksal