django settings.py中的“os.environ”无法使用apache和wsgi获取系统环境变量

时间:2022-07-14 22:44:06

I set up the django setting.py like this:

我像这样设置了django setting.py:

import os
from django.core.exceptions import ImproperlyConfigured
def get_env_variable(var_name):
    try:
        return os.environ[var_name]
    except KeyError:
        error_msg = "Set the %s environment variable" % var_name
        raise ImproperlyConfigured(error_msg)

The environment variables were configed correctly. When working with the django built in web server, everything was ok. But working with apache and wsgi, it raised an KeyError.

环境变量被正确地包含在内。使用django内置的Web服务器时,一切都很好。但是使用apache和wsgi,它引发了一个KeyError。

According to Cannot get environment variables in Django settings file, the problem is solved. But why cannot apache get the system environment variables?

根据Django设置文件中的无法获取环境变量,问题得以解决。但为什么apache不能获得系统环境变量?

UPDATED: The environment variables were set in .bashrc.

更新:环境变量在.bashrc中设置。

2 个解决方案

#1


7  

You say "The environment variables were set in .bashrc." Presumably you mean your .bashrc. Which is pointless, because Apache is not running as you, it is running as the Apache user.

你说“环境变量是在.bashrc中设置的”。大概你的意思是你的.bashrc。这没有意义,因为Apache没有像你一样运行,它作为Apache用户运行。

As explained in the blog post referenced in the very question you link to, you need to set the environment variables in the Apache configuration file itself via the SetEnv directive.

正如您链接到的问题中引用的博客文章中所述,您需要通过SetEnv指令在Apache配置文件中设置环境变量。

#2


0  

whatever environments you are setting in the settings.py will be applied for that instance's environment variables only, they are not changing the system env variables which will be called apache.

您在settings.py中设置的任何环境将仅应用于该实例的环境变量,它们不会更改将被称为apache的系统env变量。

#1


7  

You say "The environment variables were set in .bashrc." Presumably you mean your .bashrc. Which is pointless, because Apache is not running as you, it is running as the Apache user.

你说“环境变量是在.bashrc中设置的”。大概你的意思是你的.bashrc。这没有意义,因为Apache没有像你一样运行,它作为Apache用户运行。

As explained in the blog post referenced in the very question you link to, you need to set the environment variables in the Apache configuration file itself via the SetEnv directive.

正如您链接到的问题中引用的博客文章中所述,您需要通过SetEnv指令在Apache配置文件中设置环境变量。

#2


0  

whatever environments you are setting in the settings.py will be applied for that instance's environment variables only, they are not changing the system env variables which will be called apache.

您在settings.py中设置的任何环境将仅应用于该实例的环境变量,它们不会更改将被称为apache的系统env变量。