43
You must define the relevant variable to show where your settings.py
file lives:
export DJANGO_SETTINGS_MODULE=mysite.settings
This is the relevant docs entry:
When you use Django, you have to tell it which settings you’re using.
Do this by using an environment variable, DJANGO_SETTINGS_MODULE.The value of DJANGO_SETTINGS_MODULE should be in Python path syntax,
e.g. mysite.settings. Note that the settings module should be on the
Python import search path.
If you are using a virtualenv (which is the best practice), you can paste the relevant export
command in the file <path-to-virtualenv>/bin/activate
17
It happens to me too, I just now found unintentional.
I changed “configuration” in top of PyCharm IDE so pycharm confused when try to run django code. Exactly the problem popup when I try to run server with shortcut but when I used terminal to run server, I found django code not having any problem and pycharm can’t found setting.
So please check “configuration” in top pycharm’s ide maybe you do same mistake
I’m new in django so be calm if my answer was silly.
- [Django]-Django Admin – Specific user (admin) content
- [Django]-Django template tag to truncate text
- [Django]-How to add new languages into Django? My language "Uyghur" or "Uighur" is not supported in Django
17
I had this problem because I tried to import a django script after just typing
python
instead of
python manage.py shell
Duh!
- [Django]-Django admin make a field read-only when modifying obj but required when adding new obj
- [Django]-How to get request object in django unit testing?
- [Django]-Getting the SQL from a Django QuerySet
6
Problem
I had the same error with Django==3.2 and djangorestframework==3.12.4 (2021/04) when I ran unittest. In my case, the python manage.py test
works properly but I cannot directly run or debug the test module which was in a specific app, and I get this error:
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.
Solution
First I added the django project root to the sys.path
. Then I added DJANGO_SETTINGS_MODULE
to the environment variable to address the project setting root and called the Django setup
function. This is my code:
from os import path, environ
from sys import path as sys_path
from django import setup
sys_path.append(<path to django setting.py>)
environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_project.settings')
setup()
- [Django]-Django: How to get related objects of a queryset?
- [Django]-Django admin, hide a model
- [Django]-Django-AttributeError 'User' object has no attribute 'backend' (But….it does?)
4
put this in top of settings.py
this will configure django for you
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", __file__)
import django
django.setup()
- [Django]-Django-tables2: How to use accessor to bring in foreign columns?
- [Django]-Django DetailView – how to use 'request' in get_context_data
- [Django]-Get list of Cache Keys in Django
4
I had the same error with Django 3.0.8 (july 2020) when trying to run the server, and in my case no need for the DJANGO_SETTINGS_MODULE environment variable. The solution is to use another form to run the server:
cd <directory containing manage.py>
python3 manage.py runserver
It works to run Django shell too :
python3 manage.py shell
- [Django]-Django – query filter on manytomany is empty
- [Django]-Log in user using either email address or username in Django
- [Django]-Get javascript variable's value in Django url template tag
3
Like raratiru answered, you need DJANGO_SETTINGS_MODULE
environment variable defined with the relative pythonic path to your setting file.
OR use your django-admin
command with the settings
parameter:
django-admin --settings=mysite.settings dbshell
- [Django]-How can I resolve 'django_content_type already exists'?
- [Django]-Python Socket.IO client for sending broadcast messages to TornadIO2 server
- [Django]-How to compare dates in Django templates
0
Below code in terminal to manually inform which settings I am using worked for me:
set DJANGO_SETTINGS_MODULE=mysite.settings
- [Django]-Filter foreignkey field in django admin
- [Django]-Laravel's dd() equivalent in django
- [Django]-Django error: needs to have a value for field "…" before this many-to-many relationship can be used
0
Dear all I faced the same problem and i scrapped the web to find a solution. None of the one above solved anything.
The issue in my case was related to a wrong configuration of pycharm. As for someone above the app didnt have any issue when launched from the command line/ shell.
The issue is here:
For some reason the env variable disappeared. i added back and everything started to work again without any issue.
- [Django]-How do I use CSS in Django?
- [Django]-Django.db.utils.ProgrammingError: relation "bot_trade" does not exist
- [Django]-Add class to form field Django ModelForm