Django Celery application - No module named celery error

随声附和 提交于 2019-12-04 20:44:13

问题


I have created a django-celery application as in the tutorial at:

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

Everything works fine when I run it without application parameter as in:

$ python manage.py celery worker -l info

but I can't start it at all with the application parameter, as in:

$ python manage.py celery worker -A myapp -l info

where myapp is the name given to the application when I created the project with:

$ python manage.py startapp myapp

The error that I am getting is:

ImportError: No module named celery

Does anyone know why this happens and how to solve it?


回答1:


Edit April 2014:

The Celery docs have been updated for 3.1; the below solution is now outdated, see:

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


By default, celery searches for a module named celery.py to find its configuration. You can get celery to use a different name than celery.py by specify it on the app argument - in this example, we'll look for celery config in settings.py:

python manage.py celery worker --app=myapp.settings

When using django-celery you can either use the above call to start celery, or do as I originally did and create a celery.py in my application package myapp:

from settings import celery

My Django settings.py contains the normal celery config:

from celery import Celery

celery = Celery(broker="amqp://guest:guest@127.0.0.1:5672//")

celery.conf.update(
    CELERY_DEFAULT_QUEUE = "myapp",
    CELERY_DEFAULT_EXCHANGE = "myapp",
    CELERY_DEFAULT_EXCHANGE_TYPE = "direct",
    CELERY_DEFAULT_ROUTING_KEY = "myapp",
)

Then run the celery worker like this:

python manage.py celery worker --app=myapp

Just for clarity's sake, here's my full application structure:

myproject/
    manage.py
    myapp/
        __init__.py
        settings.py
        celery.py



回答2:


Be sure you're trying to start the celery worker from a directory that has access to the celery module. In my case I was trying to start the worker from the app directory rather than the project.



来源:https://stackoverflow.com/questions/15158932/django-celery-application-no-module-named-celery-error

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