trex trex - 26 days ago 12
Python Question

The SECRET_KEY setting must not be empty on Celery worker run

Django version 1.9.7.

My current project structure is:

vehicles/
├── etl
│   ├── etl
│   ├── manage.py
│   ├── pipeline
│   └── bku
└── web
├── db.sqlite3
├── manage.py
├── profiles
├── projects
├── reverse
├── static
├── templates
├── bku
│   ├── admin.py
│   ├── admin.pyc
│   ├── apps.py
│   ├── migrations
│   ├── models.py
│   ├── static
│   ├── templates
│   ├── tests.py
│   ├── urls.py
│   ├── views.py
│   └── views.pyc
└── rocket
├── celery.py
├── __init__.py
├── settings
│   ├── base.py
│   ├── dev.py
│   ├── __init__.py
│   ├── local.py
│   ├── production.py
│   ├── test.py
├── urls.py
├── wsgi.py


Now I want to use Celery in the
bku
Django app. But when I run the worker
celery -A rocket worker -l info
I get the following error
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
. I have the
SECRET_KEY
defined and I didn't have this error before trying Celery.

How can I run the worker?

rocket/celery.py

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'rocket.settings')
app = Celery('rocket')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()

@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))


rocket/init.py

from __future__ import absolute_import, unicode_literals
from .celery import app as celery_app

__all__ = ['bku']

Answer

The error message is a bit misleading—usually when you see an ImproperlyConfigured exception like that it means that Django can't find your settings file.

In your case you're setting the DJANGO_SETTINGS_MODULE environment variable to rocket.settings, but from your directory structure it looks like it should instead be something like rocket.settings.production.