将DJANGO_SETTINGS_MODULE永久设置为Windows中的环境变量 [英] Set DJANGO_SETTINGS_MODULE as an Environment Variable in Windows permanently

查看:100
本文介绍了将DJANGO_SETTINGS_MODULE永久设置为Windows中的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何永久地在WINDOWS上永久设置环境变量DJANGO_SETTINGS_MODULE并完成它?

我的意思是

  1. Win Button + Pause/Break Button
  2. 这导致Control Panel\System and Security\System
  3. 点击Advanced System Settings
  4. 点击Environment Variables
  5. 有两个框,第一个框标题为User variables,第二个框标题为System variables
  6. System variables上,单击New Button
  7. 对于放入DJANGO_IMPORT_SETTINGS
  8. 的变量名

XXX-> 我要为所有变量一次设置可变值吗?


在Django网站上,该问题 指出:

DJANGO_SETTINGS_MODULE

使用Django时,您必须告诉它所使用的设置.通过使用环境变量DJANGO_SETTINGS_MODULE来完成此操作.

DJANGO_SETTINGS_MODULE的值应采用Python路径语法,例如mysite.settings.请注意,设置模块应位于Python导入搜索路径上.


是什么意思...应该采用Python路径语法,例如mysite.settings ... 吗?

  • 我在Python所在的某个目录中: C:\Python27
  • 我在Django所在的某个目录中:C:\Python27\Lib\site-packages\django

这是什么意思.它是什么目录,表示C:\Something......

您可以一劳永逸地放置此变量,还是必须为每个项目不断更改它(我希望不是!)

这可疑的行是什么意思 请注意,设置模块应位于Python导入搜索路径上.

我想要它设置DJANGO_SETTINGS_MODULE环境变量,并从此麻烦中一劳永逸地完成

编辑

为了正常工作,仅需将Django指向一个有效的设置文件,默认情况下它将 寻找名为DJANGO_SETTINGS_MODULE的环境变量,以告诉它在哪里可以找到 设置.此变量的值应为设置文件的Python导入路径,例如 作为cms.settings.

->这是什么目录王:cms.settings?在Windows中,每个目录都以 C:\ Something ..... 开头.如何在Windows中启动这样的目录?

EDIT_2

一本书的摘录

问题 环境变量DJANGO_SETTINGS_MODULE未定义.

解决方案 运行命令python manage.py shell而不是python.

我的问题->关于什么目录?///您可以一次设置还是每个项目不同?

我的项目就像这样构造

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

我正在尝试运行models.py,并且C:\Python27\pysec-master中有一个settings.py 您可以在此处找到确切的副本.

MAYBE_IMPORTANT_EDIT

我的项目中有一个名为manage.py的文件,其中包含这些内容

#!/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)

设置变量是否需要做任何事情?我是否需要在循环中的此处设置?

编辑

对于IDLE from django.db import settings中的命令,我是否需要为PYTHON_MODULE_SETTINGS设置目录,例如C:\Python27\Lib\site-packages\django\db?

解决方案

好的,不要那么沮丧.让我们一步一步看一下:

  • Python路径语法:

    在Python中,当您在模块之间拆分代码库时,可以使用模块名称来限定导入的名称.假设您的项目的结构如下:

    my_project
    |__utils
    |  |____init__.py
    |  |__file_utils.py
    |__my_module
       |____init__.py
       |__main.py
    

    main.py中,如果要访问在file_utils.py中定义的方法,可以在main.py中添加如下的import语句:

    import utils.file_utils.read_file
    

    假定read_file是要导入到main.py的方法.这种将模块分隔为.的模块导入方式称为python路径语法.

  • PYTHONPATH:

    在上面的示例中,仅当Python解释器知道在哪里寻找第一个模块(即utils)时,import语句才有效.只有找到utils时,才能找到file_utilsread_file.您可以在环境变量PYTHONPATH中指定要让解释器查看的所有路径的列表.因此,为了在代码中包含如上所述的import语句,必须确保项目my_project的完整路径在PYTHONPATH中.假设my_project位于C:\AMAZEBALLS_CODE\my_project中,则您的PYTHONPATH

  • 中应包含C:\AMAZEBALLS_CODE
  • DJANGO_SETTINGS_MODULE:

    现在,让我们假设您的my_project实际上是Django应用程序. Django应用程序需要一个设置文件,您可以在其中指定整堆.为了指示Django要查看哪个设置文件,您可以在DJANGO_SETTINGS_MODULE中指定它.假设这是您的项目结构:

    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是Django必须在其中查找设置文件的模块.并且myroject.site_settings.dev_settings是在上述情况下需要设置为DJANGO_SETTINGS_MODULE的值.当文档说cms.settingsmysite.settings时,它们的意思是cmsmysite是您的项目的名称,而settings.py是您的设置文件的名称.

现在让我们看看您的问题:

可以在Windows的环境变量中永久设置它吗?好的.
这是正确的方法吗?否

因为如果您想明天在另一个位置创建另一个应用程序,则必须在Windows的环境变量"部分中对其进行编辑.同样,一种惯例是将不同的设置文件用于开发环境,将另一个设置文件用于生产.因此,使用一个值将其设置在一个位置会使它变得不灵活.但是,如果您了解以上所有内容,并且确定只使用了env变量部分中设置为DJANGO_SETTINGS_MODULEmyproject.site_settings.dev_settings的一个设置文件.

希望这会有所帮助!

好像您将pysec-master项目放在C:\Python27中.不要将您的项目放在python安装中.在您的项目中创建一个settings.py文件,并将DJANGO_SETTINGS_MODULE设置为pysec-master.settings

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 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 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:\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.

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:\Python27\Lib\site-packages\django\db ?

解决方案

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

这篇关于将DJANGO_SETTINGS_MODULE永久设置为Windows中的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆