Django, ImportError: cannot import name Celery, possible circular import?

前端 未结 10 530
情书的邮戳
情书的邮戳 2021-01-31 14:43

I went through this example here:

http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html

All my tasks are in files called tasks.py.

相关标签:
10条回答
  • 2021-01-31 15:11

    With Django 1.7.5, Celery 3.1.17, and Python 2.7.6 I found that I was still getting these ImportError: cannot import name Celery. But only when running tests under PyCharm 4.0.4.

    I found that a solution was not to rely on from __future__ import absolute_import as described in First Steps with Django. Instead I renamed proj/proj/celery.py to proj/proj/celery_tasks.py and then changed the content of __init__.py to match: from .celery_tasks import app as celery_app. No more multiple instances of files named celery.py to cause import confusion seemed to be a simpler approach.

    0 讨论(0)
  • 2021-01-31 15:15

    I have face similar type of issue: from celery import Celery ImportError: cannot import name 'Celery' from 'celery'

    Another simple way to solve this: If your package have celery configuration in celery.py this is the reason that it is causing problems. Rename it something like celery_settings.py

    0 讨论(0)
  • 2021-01-31 15:17

    Work for me ( some bug after deploy in server ): Remove all *.pyc files from project and restart him.

    0 讨论(0)
  • 2021-01-31 15:18

    For someone who want to know what cause this error:
    I have meet this problem just now, then I found the problem --- sys.path.
    Maybe you add some path to sys.path like me, I add below code in manage.py,

    ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
    SRC_PATH = os.path.join(ROOT_PATH, 'src')
    CONF_PATH = os.path.join(ROOT_PATH, 'conf')
    
    sys.path.insert(0, SRC_PATH)
    sys.path.insert(0, CONF_PATH)
    

    so, from celery import Celery would search celery in SRC_PATH and CONF_PATH first, that's the problem.

    change to

    sys.path.append(SRC_PATH)
    sys.path.append(CONF_PATH)
    

    It would search in python's lib and site-packages first. Solved perfectly.

    0 讨论(0)
提交回复
热议问题