Django:ImportError:无法从“芹菜”导入名称“芹菜”

时间:2019-12-28 19:37:51

标签: python django celery

因此,我已经安装了celery,并在celery_tasks_settings.py旁边的项目目录中找到了文件settings.py。我得到的是避免与包本身冲突的方法。

我也做了

find -name '*celery*.pyc'

作为查找以前可能已生成的任何celery.pyc文件的一种方法。结果:

/__pycache__/celery_tasks.cpython-38.pyc
/__pycache__/celery_tasks_settings.cpython-38.pyc

不幸的是,我仍然遇到相同的错误:

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

我做错什么了吗?

PS:我也继续尝试添加:

from __future__ import absolute_import, unicode_literals

到我的celery_tasks_settings.py

我的错误仍然不会解决。 [编辑] 完全追溯

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)

2 个答案:

答案 0 :(得分:0)

这可能是一个循环导入问题。看来您正在尝试将celery_task_settings.py中的Celery导入,这导致循环导入导致导入错误。

答案 1 :(得分:0)

我将importlib-metadata从版本0.19升级到了版本1.5.0,它为我彻底解决了这个问题。

作为参考,我有几乎相同的完整回溯:

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)

当我升级importlib-metadata时,它已完全修复。

pip install --upgrade importlib-metadata