[Django]-Django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must either define the environment varia

9👍

If you are using PyCharm Pro, you can either test your app by action ‘Run Django Console…’. After you click ‘test’, it will prompt you for the app you want to test.

enter image description here

or

Create a Django test in Run/Debug configuration.

enter image description here

👤yeh

37👍

Use this

import os

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

instead of

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<appname>.settings")

10👍

In your python script, you are trying to access Django models before setting the environment try it in this order :

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<appname>.`settings`")
from <appname>.models import Class1, Class2,...
👤Hijane

2👍

since, I don’t want to add this in every file, I created
a file called usercustomize.py and added this content:

import os
os.environ['DJANGO_SETTINGS_MODULE']='foosite.settings'

The file path in my case:

src/foosite/usercustomize.py

I installed foosite with “pip install -e”. I am unsure if this works for normal installs.

1👍

In my case I needed to use

python3 manage.py check --deploy

instead of

django-admin check --deploy
👤Samer

1👍

enter image description here

fill “working directory” correctly with your working path
then write the copy down in it’s place

👤safe89

0👍

I got this error when I imported TestCase from unittest2. I fixed it by importing TestCase by:

from django.test import TestCase

My suspicions were raised when the test was labelled “Unittest” rather than “Test”. I had to run the whole suite before Pycharm realised I’d made this change…

0👍

If you’re using PyCharm you may have forgotten to choose your debug configuration here:

PyCharm Screenshot

👤Josh

Leave a comment