[Django]-Django, ImportError: cannot import name 'task' from 'celery'

10👍

This API was deprecated, and then removed in 5.0.

That page suggests to change

from celery import task

into

from celery import shared_task

There are other changes as well which don’t apply to the snippet you’ve posted but may apply to the rest of your code. See that page (and the rest of the documentation, especially the Upgrading from Celery 4.x section) for more details.

1👍

Some older things are deprecated in Celery 5.0 and The latest version of the celery is working fine and most of the new things are added in the new version. Recommended: you need to use the latest version of the celery.

celery.py

from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'pj_name.settings')
app = Celery('pj_name')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()

after that go under the app/tasks.py and add your first scheduler function.

from pj_name.celery import app
@app.task
def first_task():
    pass

the above code block is worked if you are using celery latest version.

Leave a comment