在 Windows 中将 DJANGO_SETTINGS_MODULE 永久设置为环境变量
问题描述
如何在 WINDOWS
上永久设置环境变量 DJANGO_SETTINGS_MODULE
并完成?
我是说
Win 按钮
+暂停/中断按钮
- 这会导致
Control PanelSystem and SecuritySystem
- 点击
高级系统设置
- 点击
环境变量
- 有两个框,第一个标题为
User variables
,第二个标题为System variables
- 在
系统变量
上点击New Button
- 对于变量名放入
DJANGO_IMPORT_SETTINGS
XXX--> 我应该在变量值中添加什么来一次性设置它?
<小时>在这个 issue 的 Django 站点中,它指出:p>
DJANGO_SETTINGS_MODULE
当你使用 Django 时,你必须告诉它你正在使用哪些设置.通过使用环境变量 DJANGO_SETTINGS_MODULE 来执行此操作.
DJANGO_SETTINGS_MODULE 的值应该是 Python 路径语法,例如mysite.settings.请注意,设置模块应位于 Python 导入搜索路径中.
<小时>这是什么意思 ...应该是 Python 路径语法,例如mysite.settings... ?
- 我的
Python
位于某个目录:C:Python27
- 我的
Django
位于某个目录:C:Python27Libsite-packagesdjango
这个 mysite
是什么意思.什么目录是什么意思 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:Python27pysec-master(文件)|__local_settings.py|__manage.py|__settings.py|__C:Python27pysec(文件)|__ __init__.py|__example.py|__models.py|__xbrl.py|__xbrl_fundamentals.py
我正在尝试运行 models.py
并且我在 C:Python27pysec-master
中有一个 settings.py
您可以在这里找到准确的副本.
MAYBE_IMPORTANT_EDIT
我的项目中有一个名为 manage.py
的文件,其中包含这些内容
#!/usr/bin/env python导入操作系统导入系统如果 __name__ == "__main__":os.environ.setdefault("DJANGO_SETTINGS_MODULE", "设置")从 django.core.management 导入 execute_from_command_lineexecute_from_command_line(sys.argv)
这对设置变量有影响吗?我需要在循环内设置这里吗?
编辑
对于 IDLE from django.db import settings
中的命令,我是否需要为 PYTHON_MODULE_SETTINGS
设置一个目录,例如 C:Python27Lib站点包djangodb
?
好吧,别这么沮丧.让我们一步一步来看看:
Python 路径语法:
在 Python 中,当您跨模块拆分代码库时,您可以使用模块名称来限定导入的名称.假设您的项目结构如下:
我的项目|__实用程序||____init__.py||__file_utils.py|__my_module|____init__.py|__main.py
在你的
main.py
中,如果你想访问你在file_utils.py
中定义的方法,你可以在main.py
中添加一个 import 语句代码> 像这样:导入 utils.file_utils.read_file
假设
read_file
是您要导入main.py
的方法.这种以.
分隔每个模块的模块导入方式称为 python 路径语法.PYTHONPATH:
在上面的例子中,只有当 Python 解释器知道在哪里寻找第一个模块,即
utils
时,import 语句才会起作用.只有找到utils
才能找到file_utils
和read_file
.您可以在环境变量PYTHONPATH
中指定您希望解释器查看的所有路径的列表.因此,为了在您的代码中使用上述导入语句,您必须确保项目my_project
的完整路径位于PYTHONPATH
中.假设my_project
在C:AMAZEBALLS_CODEmy_project
你应该有C:AMAZEBALLS_CODE
在你的PYTHONPATH
DJANGO_SETTINGS_MODULE:
现在让我们假设您的
my_project
实际上是一个 Django 应用程序.Django 应用程序需要一个设置文件,您可以在其中指定一大堆东西.为了指示 Django 要查看哪个设置文件,您在DJANGO_SETTINGS_MODULE
中指定它.假设这是您的项目结构:我的项目|__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.settings
或mysite.settings
时,它们的意思是cms
或mysite
是您的项目的名称,并且settings.py
是您的设置文件的名称.
现在让我们看看你的问题:
可以在Windows的环境变量中永久设置吗?当然.
这是正确的方法吗?没有.
因为如果您想明天在另一个位置创建另一个应用程序,您必须在 Windows 的环境变量部分进行编辑.此外,对开发环境使用不同的设置文件,对生产环境使用另一个设置文件是一种做法.因此,将其设置在一个具有一个值的位置会使其不灵活.但是,如果您了解以上所有内容,并确定您将在 env 变量部分中仅使用一个设置文件集 DJANGO_SETTINGS_MODULE
到 myproject.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
Win Button
+Pause/Break Button
- This leads to
Control PanelSystem and SecuritySystem
- 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: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
?
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_CODEmy_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
相关文章