本文介绍了ImportError:对于Celery 3.1和Python 2.7,没有名为celery的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们使用Windows运行Celery worker时,在Windows上使用Python 2.7和Celery 3.1.25

Using Python 2.7 and Celery 3.1.25 on Windows, when we run the Celery worker using

celery -A proj worker -l info

我们得到了错误

ImportError: No module named celery

问题:工作人员在我们

  • celery.py
  • 更改了文件 celeryApp.py 的名称
  • tasks.py 中的import语句从.celery import app 中的更改为celeryApp import app 中的.
  • changed the name of the file celeryApp.py from celery.py
  • changed the import statement in tasks.py from from .celery import app to from celeryApp import app.

为什么会这样?我们如何解决该问题?

Why is this happening? How can we fix the problem?

目录结构

/proj/__init__.py
/proj/celeryApp.py
/proj/tasks.py

/proj/celeryApp.py

from __future__ import absolute_import, unicode_literals
from celery import Celery

app = Celery('tasks',
    broker='amqp://jack:[email protected]:5672//',
    backend='amqp://',
    include=['proj.tasks'])

if __name__ == '__main__':
    app.start()

/proj/tasks.py

from __future__ import absolute_import, unicode_literals
from celeryApp import app

@app.task
def add(x, y):
    return x + y

推荐答案

启动celery worker时,应将应用程序命名为与配置celery模块的python文件匹配.您应该以

When starting a celery worker, you should name the app to match the python file where the celery module is configured. You should start worker with

celery worker -l info -A tasks

您不应将您的配置文件命名为 celery.py .开始导入

You shouldn't name your config file to celery.py. This will cause problems when you start importing

from celery import Celery

您的文件应命名为其他名称,而不是 celery.py .

Your file should be named something else but not celery.py.

也无需在配置文件中添加

Also in your config file there is no need to add

if __name__ == '__main__':
    app.start()

如果您要明确包含 proj.tasks ,请确保 proj 在python路径中,或者您可以在开始使用 tasks进行工作时将其删除应用.

If you are going to explicitly include proj.tasks make sure proj is in python path or you can just remove it as you are starting worker with tasks app.

这篇关于ImportError:对于Celery 3.1和Python 2.7,没有名为celery的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 10:23