问题
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
fromcelery.py
- changed the import statement in
tasks.py
fromfrom .celery import app
tofrom 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.
来源:https://stackoverflow.com/questions/43032592/importerror-no-module-named-celery-for-celery-3-1-and-python-2-7