Home > Back-end >  Django Installed app and ImproperlyConfigured Error: django.core.exceptions.ImproperlyConfigured
Django Installed app and ImproperlyConfigured Error: django.core.exceptions.ImproperlyConfigured

Time:09-28

django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

Actually, I am maiking a RabbitMQ consumer in Dango Project in the same directory as the manage.py file, i am facing the above error.

I also added

import os
import pika, sys, os, json
from matcher.views import GenerateOutput

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")

These lines i got are from the stackoverflow related questions

CodePudding user response:

os.environ.setdefault() only takes action when the environment variable isn't already set. That is, if DJANGO_SETTINGS_MODULE is already set in your environment, and has a different value than what you need, this would explain your issue. Try using

os.environ["DJANGO_SETTINGS_MODULE"] = "project.settings"

CodePudding user response:

So, you are making 2 mistakes.

First: In the Code, you have to write those os.enoviron line above to the code line where you are importing the views.py function GenerateOuput

Second: You have to import the django in the code and write the code line below django.setup()

So the Complete Code will be

import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
django.setup()
import pika, sys, os, json
from matcher.views import GenerateOuput

This should work.

Happy Coding.

  • Related