ImportError: cannot import name 'Celery' from 'celery'
The code is running fine in my local machine. when i run this code on azure server then create this issue.
I also renamed celery.py file to celeryy.py file and then checked the error is still same.
here below is my project file structure in below screenshot.

__init__.py file:
from __future__ import absolute_import
from core.celery import app as celery_app
__all__ = ['celery_app']
celery.py file:
from __future__ import absolute_import
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')
app = Celery('core')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
task.py file:
from celery import shared_task
from time import sleep
from azure.datalake.store import core, lib, multithread
from django.core.mail import send_mail
token = lib.auth()
adls_client = core.AzureDLFileSystem(token, store_name='bnlweda04d3232gsdfs')
@shared_task
def sleepy(duration):
sleep(duration)
return None
@shared_task
def send_email_task(subject,message,from_email,recipient_email,fail_silently):
sleep(30)
send_mail(
subject,message,from_email,recipient_email,fail_silently
)
return 'Mail sent success'
I'm using celery version: 4.4.0 and python version: 3.8.10
I had this exact same issue the way you described, and it started happening only recently (CI failed Oct 3, 2022 but worked fine Sep 25, 2022, with no related changes in between).
What ended up being the cause was the fact that in my CI, importlib-metadata started installing version 5.0.0, which gave the error (Worked fine with 4.12.0).
To fix it for now, I just added importlib-metadata==4.13.0 to my environment.yml file, after which the issue was solved.
https://github.com/python/importlib_metadata/issues/411
Downgrade importlib-metadata to 4.13.0.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With