I’ve been trying to use celery and celery beat with my django project, but I always get the same error: django.core.exceptions.ImproperlyConfigured: Requested settings, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
and here is my celery configuration:
from celery import Celery
from datetime import timedelta
import os
from django.conf import settings
os.environ.setdefault('DJANGO_SETTING_MODULE', 'online_shop.settings')
celery_app = Celery('online_shop')
celery_app.config_from_object('django.conf:settings', namespace='CELERY')
celery_app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
celery_app.conf.broker_url = 'amqp://guest:guest@localhost'
celery_app.conf.result_backend = 'rpc'
celery_app.conf.task_serializer = 'json'
celery_app.conf.result_serializer = 'pickle'
celery_app.conf.accept_content = ['json', 'pickle']
celery_app.conf.result_expires = timedelta(days=1)
celery_app.conf.task_always_eager = False
celery_app.conf.worker_prefetch_multiplier = 2
but every time I try to run celery like this: celery -A online_shop worker -l info I get that error…
I tried this line: export export DJANGO_SETTINGS_MODULE=online_shop.settings
but this won’t work when daemonizing the process!!!
I tried writing it to the .bashrc: the same error.
I tried writing it to the activate file in virtual environment, It didn’t work either!
I also tried to export the variable before running celery in supervisor conf:
[program:exporting_to_os]
user=ali
command=export DJANGO_SETTING_MODULE=online_shop.settings
autostart=true
autorestart=true
stderr_logfile=/var/log/online_shop/exporting.err.log
stdout_logfile=/var/log/online_shop/exporting.out.log
[program:online_shop_celery]
user=ali
directory=/home/.../OnlineShop/online_shop/
command=/home/.../OnlineShop/env/bin/celery -A online_shop worker -l info
numprocs=1
autostart=true
autorestart=true
stdout_logfile=/var/log/online_shop/celery.log
stderr_logfile=/var/log/online_shop/celery.err.log
and still the same error…
Please guide me on how to solve this problem.