Django: ImportError: cannot import name 'Celery' from 'celery'
Question:
So I’ve installed celery
and got the file celery_tasks_settings.py
in my project directory next to settings.py
. This I got was a way to avoid conflicts with the package itself.
I also did
find -name '*celery*.pyc'
as a way to find any celery.pyc
files that may have been generated before.The result:
/__pycache__/celery_tasks.cpython-38.pyc
/__pycache__/celery_tasks_settings.cpython-38.pyc
Unfortunately, I still get the same error:
from celery import Celery
ImportError: cannot import name 'Celery' from 'celery'
Am I doing something wrong?
PS: I also went ahead and tried adding:
from __future__ import absolute_import, unicode_literals
to my celery_tasks_settings.py
My error still won’t budge.
[EDIT]
Full traceback
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/run/media/marvin/DriveEtCetera/Python/MyProjectsDirectory/super-lamp/.superlamp/lib/python3.8/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/run/media/marvin/DriveEtCetera/Python/MyProjectsDirectory/super-lamp/.superlamp/lib/python3.8/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/run/media/marvin/DriveEtCetera/Python/MyProjectsDirectory/super-lamp/.superlamp/lib/python3.8/site-packages/django/core/management/base.py", line 336, in run_from_argv
connections.close_all()
File "/run/media/marvin/DriveEtCetera/Python/MyProjectsDirectory/super-lamp/.superlamp/lib/python3.8/site-packages/django/db/utils.py", line 219, in close_all
for alias in self:
File "/run/media/marvin/DriveEtCetera/Python/MyProjectsDirectory/super-lamp/.superlamp/lib/python3.8/site-packages/django/db/utils.py", line 213, in __iter__
return iter(self.databases)
File "/run/media/marvin/DriveEtCetera/Python/MyProjectsDirectory/super-lamp/.superlamp/lib/python3.8/site-packages/django/utils/functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/run/media/marvin/DriveEtCetera/Python/MyProjectsDirectory/super-lamp/.superlamp/lib/python3.8/site-packages/django/db/utils.py", line 147, in databases
self._databases = settings.DATABASES
File "/run/media/marvin/DriveEtCetera/Python/MyProjectsDirectory/super-lamp/.superlamp/lib/python3.8/site-packages/django/conf/__init__.py", line 79, in __getattr__
self._setup(name)
File "/run/media/marvin/DriveEtCetera/Python/MyProjectsDirectory/super-lamp/.superlamp/lib/python3.8/site-packages/django/conf/__init__.py", line 66, in _setup
self._wrapped = Settings(settings_module)
File "/run/media/marvin/DriveEtCetera/Python/MyProjectsDirectory/super-lamp/.superlamp/lib/python3.8/site-packages/django/conf/__init__.py", line 157, 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 "/run/media/marvin/DriveEtCetera/Python/MyProjectsDirectory/super-lamp/HotelManagement/__init__.py", line 4, in <module>
from .celery_tasks_settings import app as celery_app
File "/run/media/marvin/DriveEtCetera/Python/MyProjectsDirectory/super-lamp/HotelManagement/celery_tasks_settings.py", line 4, in <module>
from celery import Celery
ImportError: cannot import name 'Celery' from 'celery' (/run/media/marvin/DriveEtCetera/Python/MyProjectsDirectory/super-lamp/.superlamp/lib/python3.8/site-packages/celery/__init__.py)
Answers:
This is probably a circular import issue. It looks like you are trying to import Celery
in the celery_task_settings.py and that is causing a circular import leading to the import error.
I upgraded importlib-metadata from verion 0.19 to version 1.5.0 and it totally resolved this issue for me.
For reference, I had an almost identical full traceback:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/mnt/SSD/uvo/help/voiceover_backend/env/lib/python3.8/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/mnt/SSD/uvo/help/voiceover_backend/env/lib/python3.8/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/mnt/SSD/uvo/help/voiceover_backend/env/lib/python3.8/site-packages/django/core/management/base.py", line 336, in run_from_argv
connections.close_all()
File "/mnt/SSD/uvo/help/voiceover_backend/env/lib/python3.8/site-packages/django/db/utils.py", line 219, in close_all
for alias in self:
File "/mnt/SSD/uvo/help/voiceover_backend/env/lib/python3.8/site-packages/django/db/utils.py", line 213, in __iter__
return iter(self.databases)
File "/mnt/SSD/uvo/help/voiceover_backend/env/lib/python3.8/site-packages/django/utils/functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/mnt/SSD/uvo/help/voiceover_backend/env/lib/python3.8/site-packages/django/db/utils.py", line 147, in databases
self._databases = settings.DATABASES
File "/mnt/SSD/uvo/help/voiceover_backend/env/lib/python3.8/site-packages/django/conf/__init__.py", line 79, in __getattr__
self._setup(name)
File "/mnt/SSD/uvo/help/voiceover_backend/env/lib/python3.8/site-packages/django/conf/__init__.py", line 66, in _setup
self._wrapped = Settings(settings_module)
File "/mnt/SSD/uvo/help/voiceover_backend/env/lib/python3.8/site-packages/django/conf/__init__.py", line 157, 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 "/mnt/SSD/uvo/help/voiceover_backend/voiceover_backend/__init__.py", line 5, in <module>
from .celery import app as celery_app
File "/mnt/SSD/uvo/help/voiceover_backend/voiceover_backend/celery.py", line 3, in <module>
from celery import Celery
ImportError: cannot import name 'Celery' from 'celery' (/mnt/SSD/uvo/help/voiceover_backend/env/lib/python3.8/site-packages/celery/__init__.py)
It got totally fixed when I upgraded importlib-metadata.
pip install --upgrade importlib-metadata
Posted this answer on another question, but it actually suits better here. So again:
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.
So I’ve installed celery
and got the file celery_tasks_settings.py
in my project directory next to settings.py
. This I got was a way to avoid conflicts with the package itself.
I also did
find -name '*celery*.pyc'
as a way to find any celery.pyc
files that may have been generated before.The result:
/__pycache__/celery_tasks.cpython-38.pyc
/__pycache__/celery_tasks_settings.cpython-38.pyc
Unfortunately, I still get the same error:
from celery import Celery
ImportError: cannot import name 'Celery' from 'celery'
Am I doing something wrong?
PS: I also went ahead and tried adding:
from __future__ import absolute_import, unicode_literals
to my celery_tasks_settings.py
My error still won’t budge.
[EDIT]
Full traceback
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/run/media/marvin/DriveEtCetera/Python/MyProjectsDirectory/super-lamp/.superlamp/lib/python3.8/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/run/media/marvin/DriveEtCetera/Python/MyProjectsDirectory/super-lamp/.superlamp/lib/python3.8/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/run/media/marvin/DriveEtCetera/Python/MyProjectsDirectory/super-lamp/.superlamp/lib/python3.8/site-packages/django/core/management/base.py", line 336, in run_from_argv
connections.close_all()
File "/run/media/marvin/DriveEtCetera/Python/MyProjectsDirectory/super-lamp/.superlamp/lib/python3.8/site-packages/django/db/utils.py", line 219, in close_all
for alias in self:
File "/run/media/marvin/DriveEtCetera/Python/MyProjectsDirectory/super-lamp/.superlamp/lib/python3.8/site-packages/django/db/utils.py", line 213, in __iter__
return iter(self.databases)
File "/run/media/marvin/DriveEtCetera/Python/MyProjectsDirectory/super-lamp/.superlamp/lib/python3.8/site-packages/django/utils/functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/run/media/marvin/DriveEtCetera/Python/MyProjectsDirectory/super-lamp/.superlamp/lib/python3.8/site-packages/django/db/utils.py", line 147, in databases
self._databases = settings.DATABASES
File "/run/media/marvin/DriveEtCetera/Python/MyProjectsDirectory/super-lamp/.superlamp/lib/python3.8/site-packages/django/conf/__init__.py", line 79, in __getattr__
self._setup(name)
File "/run/media/marvin/DriveEtCetera/Python/MyProjectsDirectory/super-lamp/.superlamp/lib/python3.8/site-packages/django/conf/__init__.py", line 66, in _setup
self._wrapped = Settings(settings_module)
File "/run/media/marvin/DriveEtCetera/Python/MyProjectsDirectory/super-lamp/.superlamp/lib/python3.8/site-packages/django/conf/__init__.py", line 157, 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 "/run/media/marvin/DriveEtCetera/Python/MyProjectsDirectory/super-lamp/HotelManagement/__init__.py", line 4, in <module>
from .celery_tasks_settings import app as celery_app
File "/run/media/marvin/DriveEtCetera/Python/MyProjectsDirectory/super-lamp/HotelManagement/celery_tasks_settings.py", line 4, in <module>
from celery import Celery
ImportError: cannot import name 'Celery' from 'celery' (/run/media/marvin/DriveEtCetera/Python/MyProjectsDirectory/super-lamp/.superlamp/lib/python3.8/site-packages/celery/__init__.py)
This is probably a circular import issue. It looks like you are trying to import Celery
in the celery_task_settings.py and that is causing a circular import leading to the import error.
I upgraded importlib-metadata from verion 0.19 to version 1.5.0 and it totally resolved this issue for me.
For reference, I had an almost identical full traceback:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/mnt/SSD/uvo/help/voiceover_backend/env/lib/python3.8/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/mnt/SSD/uvo/help/voiceover_backend/env/lib/python3.8/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/mnt/SSD/uvo/help/voiceover_backend/env/lib/python3.8/site-packages/django/core/management/base.py", line 336, in run_from_argv
connections.close_all()
File "/mnt/SSD/uvo/help/voiceover_backend/env/lib/python3.8/site-packages/django/db/utils.py", line 219, in close_all
for alias in self:
File "/mnt/SSD/uvo/help/voiceover_backend/env/lib/python3.8/site-packages/django/db/utils.py", line 213, in __iter__
return iter(self.databases)
File "/mnt/SSD/uvo/help/voiceover_backend/env/lib/python3.8/site-packages/django/utils/functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/mnt/SSD/uvo/help/voiceover_backend/env/lib/python3.8/site-packages/django/db/utils.py", line 147, in databases
self._databases = settings.DATABASES
File "/mnt/SSD/uvo/help/voiceover_backend/env/lib/python3.8/site-packages/django/conf/__init__.py", line 79, in __getattr__
self._setup(name)
File "/mnt/SSD/uvo/help/voiceover_backend/env/lib/python3.8/site-packages/django/conf/__init__.py", line 66, in _setup
self._wrapped = Settings(settings_module)
File "/mnt/SSD/uvo/help/voiceover_backend/env/lib/python3.8/site-packages/django/conf/__init__.py", line 157, 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 "/mnt/SSD/uvo/help/voiceover_backend/voiceover_backend/__init__.py", line 5, in <module>
from .celery import app as celery_app
File "/mnt/SSD/uvo/help/voiceover_backend/voiceover_backend/celery.py", line 3, in <module>
from celery import Celery
ImportError: cannot import name 'Celery' from 'celery' (/mnt/SSD/uvo/help/voiceover_backend/env/lib/python3.8/site-packages/celery/__init__.py)
It got totally fixed when I upgraded importlib-metadata.
pip install --upgrade importlib-metadata
Posted this answer on another question, but it actually suits better here. So again:
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.