Set DJANGO_SETTINGS_MODULE as an Enviroment Variable in Windows permanently
Question: I have searched Web and Stack to this solution.

How can i permanently set the environmental variable `DJANGO_SETTINGS_MODULE`
on `WINDOWS` on a permanent basis and be done with it.

I mean

  1. `Win Button` \+ `Pause/Break Button`
  2. This leads to `Control Panel\System and Security\System`
  3. Click `Advanced System Settings`
  4. Click `Environment Variables`
  5. There are two boxes the first is titled `User variables` and the second `System variables`
  6. On the `System variables` click the `New Button`
  7. 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](https://docs.djangoproject.com/en/dev/topics/settings/) 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 on earth 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 meanning
`C:\Something......`

Can you put this variable once and for all or you have to constantly change it
for every project(Holly Moses 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**

They should really make tutorials for coding enthusiasts too, not only
experienced hardened professionals!

Thank you.

**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 than `python`._

**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 a `settings.py` in the
`C:\Python27\pysec-master` You can find an exact copy
[here](https://github.com/lukerosiak/pysec).

**MAYBE_IMPORTANT_EDIT**

Guys 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 the `PYTHON_MODULE_SETTINGS` like `C:\Python27\Lib\site-
packages\django\db` ?

THANK YOU


Answer: 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 in
`file_utils.py` you add an import statement in your `main.py` like this:

    
        import utils.file_utils.read_file
    

assuming `read_file` is the method you want to import into `main.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 finds `utils` can it find `file_utils` and `read_file`. You specify
the list of all the paths you want the interpreter to look into in the
environment variable `PYTHONPATH`. So in order to have an import statement
like above in your code, you have to make sure that the full path to your
project `my_project` is in `PYTHONPATH`. Assuming `my_project` is in
`C:\AMAZEBALLS_CODE\my_project` you should have `C:\AMAZEBALLS_CODE` in your
`PYTHONPATH`

  * **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](https://docs.djangoproject.com/en/dev/topics/settings/). In order to
instruct Django which settings file to look into you specify it in
`DJANGO_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. And `myroject.site_settings.dev_settings` is the value you need
to be setting to the `DJANGO_SETTINGS_MODULE` in the above case. When
documentation says `cms.settings` or `mysite.settings` they mean `cms` or
`mysite` is the name of your project and `settings.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`

