Make Celery ignore Django settings

I wanted to run an instance of Celery on my Linux machine. I installed it in a separate virtual environment (celery-test), but when I tried to run a sample application, I’ve got an error

ImportError: No module named xxx.settings

It was obvious that the problem was that I had Django installed system wide, so even from its own virtualenv my Celery could see Django settings. So I checked if I have references to Django in my environment variables:

printenv | grep xxx
DJANGO_SETTINGS_MODULE=xxx.settings

Let’s get rid of Django references for this particular virtualenv. Virtual environments have hooks for different types of events like preactivate or postdeactivate. They are stored in separate files in the virtualenv folder:

ls $VIRTUAL_ENV/bin

In my case I decided to delete Django environment variables after I activate my Celery virtualenv and set them back when I deactivate it. To do that I had to put the following command to $VIRTUAL_ENV/bin/postactivate :

unset DJANGO_SETTINGS_MODULE

And modify $VIRTUAL_ENV/bin/postdeactivate to set variables back:

DJANGO_SETTINGS_MODULE=xxx.settings

Now don’t forget to re-activate the virtualenv and the Celery should run fine:

deactivate  # deactivate virtualenv
workon celery-test  # activate virtualenv

celery worker -A tasks  # run celery

 

Leave a Reply

Your email address will not be published. Required fields are marked *