ImproperlyConfigured: You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings
I figured that the DJANGO_SETTINGS_MODULE had to be set some way, so I looked at the documentation (link updated) and found:
export DJANGO_SETTINGS_MODULE=mysite.settings
Though that is not enough if you are running a server on heroku, you need to specify it there, too. Like this:
heroku config:set DJANGO_SETTINGS_MODULE=mysite.settings --account <your account name>
In my specific case I ran these two and everything worked out:
export DJANGO_SETTINGS_MODULE=nirla.settings
heroku config:set DJANGO_SETTINGS_MODULE=nirla.settings --account personal
Edit
I would also like to point out that you have to re-do this every time you close or restart your virtual environment. Instead, you should automate the process by going to venv/bin/activate and adding the line: set DJANGO_SETTINGS_MODULE=mysite.settings
to the bottom of the code. From now on every time you activate the virtual environment, you will be using that app's settings.
You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings. Django
You can assign your settings file in manage.py
:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
manage.py:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault('DJANGO_SETTINGS_MODULE',
'djangoproject.settings.dev')
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
Or you can use the --settings
command-line argument to specify the settings manually:
python manage.py runserver --settings=mysite.settings
See DJANGO_SETTINGS_MODULE if you need more info.
django.core.exceptions.ImprperlyConfigured solution
This problem is simply because Django doesn't know where to find required settings.
As it's [Documentation][1] mentions :
When you use Django, you have to tell it which settings you’re using. Do this
by using an environment variable, DJANGO_SETTINGS_MODULE.
based on Documentation you should do that (informing Django about the setting)by defining a environmental variable.
as I see that there are little ambiguities about what to do facing this error a list of different solutions you can take, is provided :
1- define a environment variable in the operating system([link][2]).
2- if you are using a virtual environment; set a new environment variable in activate.bat
set "DJANGO_SETTINGS_MODULE=<your_proj_name>.settings"
in this way, whenever you activate the virtual environment that variable would be defined automatically.
3- (the way you did!)in each python file; write the command which defines that environment variable.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '<your_proj_name>.settings')
but if you use the third method, it is really important where you are putting that command(in the second and definitely first case we don't care at all!).
so you should take care of not using Django before that command in your script.
(some people wonder why after using this command still we are getting the same error. the reason is they put that line of code in a place where Django was used before)
you might ask why when you were using migration and so on; you didn't face this error. the reason would be clear if we take a look at manage.py
because there is that command there(third method of the above list)
but you
[1]: https://docs.djangoproject.com/en/4.0/topics/settings/#designating-the-settings
[2]: https://www.computerhope.com/issues/ch000549.htm
How to resolve django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS error?
I just move the
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'first_django_project.settings')
django.setup()
before the from first_app.models import *
Django settings module is defined but still not working
Turns out it was a problem with the script file and believe it or not it was a super simple fix. I was trying to import the Model before even calling the os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'QuickChemistry.settings')
Instead of doing:
from quickapp.models import ChemProblems
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE",'QuickChemistry.settings')
import django
django.setup()
import random
import csv
I called the import AFTER I called the settings file like this
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE",'QuickChemistry.settings')
import django
django.setup()
import random
import csv
from quickapp.models import ChemProblems # Call the import AFTER
Related Topics
Python: Best Practice and Securest Way to Connect to MySQL and Execute Queries
Separation of Business Logic and Data Access in Django
How to Remove Packages Installed with Python's Easy_Install
How to Account for Period (Am/Pm) Using Strftime
How to Set the Aspect Ratio in Matplotlib
What's the Fastest Way in Python to Calculate Cosine Similarity Given Sparse Matrix Data
How to Override the [] Operator in Python
Determine If Python Is Running Inside Virtualenv
How to Implement Server Push in Flask Framework
What's the Difference Between %S and %D in Python String Formatting
What Is the Point of Indexing in Pandas
Validate Ssl Certificates with Python
How to Set Class Attributes from Variable Arguments (Kwargs) in Python
What Is the Return Value of Os.System() in Python