Search code examples
django-celery

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?


Solution

  • 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