ImportError: cannot import name 'Celery'

Question:

I’m trying to learn Celery i’m using Django 2.0 and celery 5.0.2 and my os is Ubuntu.

This is my structure
My project structure is:

celery/
  manage.py
  celery/
    __init__.py
    cerely_app.py
    settings.py
    urls.py
    wsgi.py
  apps/
      main/ 
        __init__.py
        admin.py
        apps.py
        models.py
        task.py
        views.py
        test.py          

My configuration for cerely_app, based on documentation:

import os
from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'celery.settings')

app = Celery('celery')

app.config_from_object('django.conf:settings', namespace='CELERY')

app.autodiscover_tasks()


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

And my init.py:

from .celery_app import app as celery_app
__all__ = ('celery_app',)

But when django give a error of import when i use command python3 manage.py runserver:

$python3 manage.py runserver
Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/home/brayan/Envs/celery/lib/python3.8/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/home/brayan/Envs/celery/lib/python3.8/site-packages/django/core/management/__init__.py", line 317, in execute
    settings.INSTALLED_APPS
  File "/home/brayan/Envs/celery/lib/python3.8/site-packages/django/conf/__init__.py", line 56, in __getattr__
    self._setup(name)
  File "/home/brayan/Envs/celery/lib/python3.8/site-packages/django/conf/__init__.py", line 43, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/brayan/Envs/celery/lib/python3.8/site-packages/django/conf/__init__.py", line 106, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/brayan/dev/python/celery/celery/celery/__init__.py", line 3, in <module>
    from .celery_app import app as celery_app
  File "/home/brayan/dev/python/celery/celery/celery/celery_app.py", line 2, in <module>
    from celery import Celery
ImportError: cannot import name 'Celery' from partially initialized module 'celery' (most likely due to a circular import) (/home/brayan/dev/python/celery/celery/celery/__init__.py)
Asked By: brayan milian

||

Answers:

Do not put same name of your package and system package as it creates confusion for python when you hit import statement. In your case you name your package celery which is also a name of original celery package. In short simply rename your celery folder to something else.

Answered By: Puneet Jain

I recently upgraded from Python 3.6 to Python 3.7 and I am using Celery 4.4.6 and Django 2.2.x and it also gave me a similar error:

ImportError: cannot import name 'Celery' from 'celery'

However, as I had my project setup as told in the docs it was referring to the right package. Only after some lost hours I stumbled accross this Issue here. I then fixed

importlib-metadata==4.8.3

and the problem was resolved. According to GitHub all versions < 5 might resolve the issue. Apparently Celery uses an interface from this package on specific Python versions, which they deprecated in version 5.

Answered By: sidi7
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.