Django NotperlyConfigured - SECRET_KEY parameter must not be empty

I am running a django application using celery. I get a strange error in the tasks.py file that looks like this:

from __future__ import absolute_import

from celery import shared_task
from django.contrib.auth.models import User

# some code 
# ....

Here is the error:

django.core.exceptions.ImproperlyConfigured: The SECRET_KEY must not be empty.

SECRET_KEY is defined in my settings.py

# ...
import myproj.tasks
import djcelery

djcelery.setup_loader()

# ...
SECRET_KEY = '18730s9n9sjxamsuJSismxOIAmso102xjAs'
# ...

The application works fine if I comment on the import in the tasks.py file:

# from django.contrib.auth.models import User

and, more surprisingly, it works fine if I do the exact import in another file (e.g. testfile.py) in the same directory.

Why is this error occurring?

Edit: Here is my celery.py file

from __future__ import absolute_import
import os
from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproj.settings')

from django.conf import settings #noqa

app = Celery('myproj')

app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
+4
source share
2 answers

, CELERYBEAT_SCHEDULE.

, , , , , , , SECRET_KEY.

( ), CELERYBEAT_SCHEDULE :

CELERYBEAT_SCHEDULE = {
    'add-every-30-seconds': {
        'task': 'myapp.tasks.my_task',
        'schedule': timedelta(seconds=30),
        'args': (16, 16)
    },
}

http://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html#entries

+5

, Celery , Django. :

# celery.py
from __future__ import absolute_import

import os
from celery import Celery

from django.conf import settings

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'yourapp.settings')
app = Celery('yourapp')
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

:

# tasks.py
from yourapp.celery import app

@app.task()
def yourtask():
    pass
0
source

Source: https://habr.com/ru/post/1623811/


All Articles