Django Installed app and ImproperlyConfigured Error: django.core.exceptions.ImproperlyConfigured

Question:

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

Asked By: Kashif Usman

||

Answers:

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"
Answered By: David Schultz

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.

Answered By: Shahmeer Khan
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.