这是我正在运行的代码:
from __future__ import absolute_import
from celery import Celery
celery1 = Celery('celery',broker='amqp://',backend='amqp://',include=['tasks'])
celery1.conf.update(
CELERY_TASK_RESULT_EXPIRES=3600,
)
if __name__ == '__main__':
celery1.start()
当我执行上面的代码时,它给了我以下错误:
ImportError: cannot import name Celery
最佳答案
我也遇到了同样的错误,重命名文件修复了它。
对于遇到此问题的其他人,您遇到此问题的原因是您的本地 celery.py 被导入而不是实际的 celery 包,因此 python 找不到 Celery(capital "C") 的原因,如celery 包中的类,但它确实找到了 celery(小写“c”),这是您本地的 celery.py,其中没有定义 Celery 类。
我刚刚将我的本地 celery.py 重命名为 _celery.py,但 celery.py 以外的任何名称都可以解决此问题。
编辑: 我还想提一下,在 celery.py 文件所在的目录之外调用 celery 守护进程也可以。出于测试目的,重命名文件就足够了,但您仍然可以使用 celery.py 作为本地文件名。例如,给定以下文件夹结构:
+ root/ - proj/ * celery.py * tasks.py
If you call celery from the root folder, you should be able to type:
celery worker --app=proj [optional args]
您可以使用“-l info”可选日志参数验证您的任务是否存在,并直接查看介绍数据下方的 [tasks] 列表。
更多信息在此处的教程文档中:http://docs.celeryproject.org/en/latest/getting-started/next-steps.html#about-the-app-argument
关于python - celery 中的导入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19673662/