Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

python - Set DJANGO_SETTINGS_MODULE as an Environment Variable in Windows permanently

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 PanelSystem and SecuritySystem
  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 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:Python27Libsite-packagesdjango

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 (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 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:Python27pysec-master(file)
|__local_settings.py
|__manage.py
|__settings.py
|__C:Python27pysec(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:Python27pysec-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 the PYTHON_MODULE_SETTINGS like C:Python27Libsite-packagesdjangodb ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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_CODEmy_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. 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...