Set DJANGO_SETTINGS_MODULE as an Environment Variable in Windows permanently
Solution 1
Okay, don't be so frustrated. Let's look at this step by step:
-
Python path syntax:
In Python, when you split your code base across modules, you qualify the name of the import with the name of the module. Let's say your project is structured like this:
my_project |__utils | |____init__.py | |__file_utils.py |__my_module |____init__.py |__main.py
In your
main.py
if you want to access methods you have defined infile_utils.py
you add an import statement in yourmain.py
like this:import utils.file_utils.read_file
assuming
read_file
is the method you want to import intomain.py
. This way of importing modules where you have a.
separating every module is referred as python path syntax. -
PYTHONPATH:
In the above example, the import statement would work only if the Python interpreter knows where to look for the first module namely the
utils
. Only when it findsutils
can it findfile_utils
andread_file
. You specify the list of all the paths you want the interpreter to look into in the environment variablePYTHONPATH
. So in order to have an import statement like above in your code, you have to make sure that the full path to your projectmy_project
is inPYTHONPATH
. Assumingmy_project
is inC:\AMAZEBALLS_CODE\my_project
you should haveC:\AMAZEBALLS_CODE
in yourPYTHONPATH
-
DJANGO_SETTINGS_MODULE:
Now let's suppose your
my_project
is actually a Django application. A Django application needs a settings file where you specify a whole bunch of things. In order to instruct Django which settings file to look into you specify it inDJANGO_SETTINGS_MODULE
. Assuming this is your project structure:my_project |__utils/ | |____init__.py | |__file_utils.py |__my_module/ | |____init__.py | |__main.py |__site_settings/ |__dev_settings.py |__production_settings.py
myroject.site_settings
is the module Django has to look into for the settings file. Andmyroject.site_settings.dev_settings
is the value you need to be setting to theDJANGO_SETTINGS_MODULE
in the above case. When documentation sayscms.settings
ormysite.settings
they meancms
ormysite
is the name of your project andsettings.py
is the name of your settings file.
Now let's look at your question:
Can you permanently set it in the environment variables of Windows? Sure.
Is it the right way? No.
Because if you want to create another application tomorrow in another location, you will have to edit this in the environment variables section of Windows. Also, it is a practice to use a different settings file for development environment and another one for production. So setting it at one place with one value makes it inflexible. But if you are aware of all of the above and sure you are gonna be using just the one settings file set DJANGO_SETTINGS_MODULE
to myproject.site_settings.dev_settings
in the env variables section.
Hope this helps!
EDIT:
Looks like you are putting your pysec-master
project in C:\Python27
. Do not put your projects in the python installation. Create a settings.py
file in your project and set DJANGO_SETTINGS_MODULE
to pysec-master.settings
Solution 2
You said:
All i want it to set the DJANGO_SETTINGS_MODULE environmental variable and be done once and for all from this hassle
If you don't want to go through a tedious procedure every time, you don't have to make it permanent, you only need to make the procedure automatic.
Create a bat and put set DJANGO_SETTINGS_MODULE='xxx.settings'
inside.
If you are using virtualenv, you can set the environment variable in bin/activate.bat.
ExoticBirdsMerchant
The time will soon come where i will stop pets emporeum and start business intelligence software company... Hail user2140261 Respect Kids Believe in God
Updated on July 24, 2022Comments
-
ExoticBirdsMerchant almost 2 years
How can I permanently set the environmental variable
DJANGO_SETTINGS_MODULE
onWINDOWS
on a permanent basis and be done with it?I mean
-
Win Button
+Pause/Break Button
- This leads to
Control Panel\System and Security\System
- Click
Advanced System Settings
- Click
Environment Variables
- There are two boxes the first is titled
User variables
and the secondSystem variables
- On the
System variables
click theNew Button
- For variable name put in
DJANGO_IMPORT_SETTINGS
XXX--> WHAT DO I PUT IN VARIABLE VALUE TO SET IT ONCE AND FOR ALL?
In the Django Site on this issue it states:
DJANGO_SETTINGS_MODULE
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.
What does it mean ...should be in Python path syntax e.g. mysite.settings... ?
- I have a certain directory where my
Python
is located:C:\Python27
- I have a certain directory where my
Django
is located:C:\Python27\Lib\site-packages\django
What does this
mysite
means. What directory is it meanningC:\Something......
Can you put this variable once and for all or you have to constantly change it for every project (I hope not!)
And what does this suspiciously line means Note that the settings module should be on the Python import search path.
All I want it to set the DJANGO_SETTINGS_MODULE environmental variable and be done once and for all from this hassle
EDIT
In order to work, Django just has to be pointed at a valid settings file, and by default it looks for an environment variable named DJANGO_SETTINGS_MODULE to tell it where to find the settings. The value of this variable should be the Python import path of the settings file, such as cms.settings.
--> What king of directory is this:
cms.settings
? In windows every directory starts with a hard drive as C:\Something...... How can you start a directory like this in Windows?EDIT_2
Excerpt from a book
PROBLEM Environment variable DJANGO_SETTINGS_MODULE is undefined.
SOLUTION Run the command
python manage.py shell
rather thanpython
.MY QUESTION --> ON WHAT DIRECTORY?///CAN YOU SET IT FOR ONCE OR IS IT DIFFERENT PER PROJECT?
MY PROJECT IS STRUCTURED LIKE THIS
C:\Python27\pysec-master(file) |__local_settings.py |__manage.py |__settings.py |__C:\Python27\pysec(file) |__ __init__.py |__example.py |__models.py |__xbrl.py |__xbrl_fundamentals.py
I am trying to run
models.py
and I have asettings.py
in theC:\Python27\pysec-master
You can find an exact copy here.MAYBE_IMPORTANT_EDIT
I have a file called
manage.py
in my project which has these contents#!/usr/bin/env python import os import sys if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings") from django.core.management import execute_from_command_line execute_from_command_line(sys.argv)
Does this has to do anything on setting the variable? Do I need to set here here inside the loop?
EDIT
For the command in the IDLE
from django.db import settings
do i need to set a directory for thePYTHON_MODULE_SETTINGS
likeC:\Python27\Lib\site-packages\django\db
? -