7

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)

MarvinKweyu
  • 180
  • 4
  • 12

3 Answers3

5

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
MarvinKweyu
  • 180
  • 4
  • 12
Llama D'Attore
  • 323
  • 1
  • 12
  • 2
    You, my friend, deserve a round on me. `pip install --upgrade importlib-metadata ` does the charm. – MarvinKweyu Feb 23 '20 at 09:42
  • 3
    Thank you soooo much for this! I actually had the exact opposite problem: my installed copy of `importlib-metadata` was too *new*, and that was breaking the celery import. Downgrading from 5.0.0 to 4.13.0 fixed it. I'm using Python 3.7 in this docker image, which may be related. – coredumperror Oct 05 '22 at 22:27
  • Same here! Took me too long to figure this out, so I posted an answer here: https://stackoverflow.com/a/74136587/10180115 importlib-metadata version 5 and celery break on specific Python versions (in my case Python 3.7). – sidi7 Oct 20 '22 at 08:24
1

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.

sidi7
  • 393
  • 4
  • 11
0

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.

2ps
  • 15,099
  • 2
  • 27
  • 47
  • If I'm not wrong, that's how one should work with `celery`. Importing the module in a file and only then importing your file in the project's init so celery can work – MarvinKweyu Dec 31 '19 at 12:57
  • If you want help with the circular import, please post your celery_task_settings.py, celery.py, and django settings files. – 2ps Dec 31 '19 at 20:38
  • `celery.py` would be the standard package. My celery_task_settings file, however, has been included in the updated answer. Could you be more specific on the section needed in the settings file? Quite large. – MarvinKweyu Jan 01 '20 at 18:51