Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Celery application - No module named celery error

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?

like image 984
Clara Avatar asked Mar 01 '13 13:03

Clara


People also ask

Can I use Celery without Django?

Yes you can. Celery is a generic asynchronous task queue. In place of "django_project" you would point to your module.

Does Django Celery work on Windows?

If we try to run Celery on Windows, we will run into a problem: Windows is not officially supported by Celery.

What is Celery module in Python?

Celery is an open-source Python library which is used to run the tasks asynchronously. It is a task queue that holds the tasks and distributes them to the workers in a proper manner. It is primarily focused on real-time operation but also supports scheduling (run regular interval tasks).


2 Answers

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:[email protected]: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
like image 72
mafrosis Avatar answered Oct 23 '22 04:10

mafrosis


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.

like image 25
Raj Avatar answered Oct 23 '22 05:10

Raj