ImportError: No module named celery for Celery 3.1 and Python 2.7

匿名 (未验证) 提交于 2019-12-03 01:00:01

问题:

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

celery -A proj worker -l info 

we get the error

ImportError: No module named celery 

Problem: The worker stops working when we

  • 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?

Directory structure

/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:jack@192.168.1.26: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 

回答1:

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 

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

from celery import Celery 

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() 

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.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!