12

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?

4
  • 3
    -A argument is for celery applications, not for Django applications... They are not used by django-celery at the moment. Commented Apr 9, 2013 at 14:13
  • ah, I didn't know that. Thanks for the answer. But then how can I run this? Commented Apr 9, 2013 at 14:49
  • Celery 3.1 will support django from scratch (but does not have all of the features from django-celery, e.g no database result backend and no database periodic task scheduler, but you can still use djcelery on top to get that) Commented Apr 10, 2013 at 10:43
  • Example using celery with django in celery 3.1 (dev): github.com/celery/celery/tree/master/examples/django Commented Apr 10, 2013 at 10:44

2 Answers 2

17

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
Sign up to request clarification or add additional context in comments.

6 Comments

i'm voting +1 because this helped me realize that celery config is a bit of black magic and I have to be careful. But you should edit your answer because it's inconsistent. Why do you have a celery.py file if you're setting up celery in settings.py? Or did you mean to say you're setting it up in celery.py?
Just updated the answer, based on a better understanding I now have :) @Milimetric I have a celery.py file which imports settings.py, as that is what celery looks for by default.
If your code is working, and it's open source, a link would be great. Thanks for the edit.
@Milimetric Sorry I don't have a link to a specific example. I use Flask on open-source projects.. Can you not get the above working?
no, i'm having some crazy unrelated problem. I have from celery import Celery in a folder along with what's probably bad configuration. Here's the crazy part: when I try to run any other working examples, it goes to that other folder and says ImportError: cannot import name Celery
|
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.