简体   繁体   English

为什么python不能读取我的环境变量?

[英]Why can't python read my environment variables?

I'm working with Django and I'm trying out to set several environment variables that define which settings are imported. 我正在使用Django,我正在尝试设置几个环境变量来定义导入的设置。

I use the following folder herarchy: 我使用以下文件夹herarchy:

project_folder>
    app_folder>
    project_specific_files>
        settings>
            base.py
            local.py

I use Kenneth Reitz's autoenv to load my environment variables. 我使用Kenneth Reitz的autoenv来加载我的环境变量。 I know it's working correctly because I can type $ echo $DJANGO_SETTINGS_MODULE and I see this output: 我知道它工作正常,因为我可以键入$ echo $DJANGO_SETTINGS_MODULE ,我看到这个输出:

project_specific_files.settings.local

But when I run $django-admin.py runserver I get: 但是当我运行$django-admin.py runserver我得到:

ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.

Also, if I go into python shell and try to print out the env variables, I get the following: 另外,如果我进入python shell并尝试打印出env变量,我会得到以下结果:

Python 2.7.3 (default, Dec 22 2012, 21:14:12) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> print os.environ['HOME']
/home/marco
>>> print os.environ['DJANGO_SETTINGS_MODULE']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/marco/.virtualenvs/myproject/lib/python2.7/UserDict.py", line 23, in __getitem__
    raise KeyError(key)
KeyError: 'DJANGO_SETTINGS_MODULE'

Anyone have any clue what could be wrong? 任何人都有任何线索可能是错的? Thanks! 谢谢!

Also, I can type $ django-admin.py runserver --settings='project_specific_files.settings.local and everything will work alright 0.o 另外,我可以键入$ django-admin.py runserver --settings='project_specific_files.settings.local ,一切都会正常工作0.o

Is your environment variable exported? 您的环境变量是否已导出?

Maybe the environment variable is defined in the current shell, but not marked as to be exported to sub processes (the python interpreter, or Django). 也许环境变量是在当前shell中定义的,但没有标记为要导出到子进程(python解释器或Django)。

With bash, run: export DJANGO_SETTINGS_MODULE 使用bash,运行: export DJANGO_SETTINGS_MODULE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM