1. 两个框中设置配置环境变量文件(.env)的方式都可以。
2. 如果选第一种,DOT_ENV_FILE变量的名字一定要这样,不能改。
3. setting.py中的 environ.Env.read_env('heat_conf/settings/production.env'),只有在上述两种方式都未配置的情况下才会生效(优先级低,生产环境非部署的docker环境,所以未配置前两种方式,因此生效)
贴上environ.Env.read_env的部分代码:
# 将 .env 文件中的配置设置到环境变量中
try:
with open(env_file) if isinstance(env_file, basestring) else env_file as f:
content = f.read()
except IOError:
warnings.warn(
"Error reading %s - if you're not configuring your "
"environment separately, check this." % env_file)
return
logger.debug('Read environment variables from: {0}'.format(env_file))
for line in content.splitlines():
m1 = re.match(r'\A(?:export )?([A-Za-z_0-9]+)=(.*)\Z', line)
if m1:
key, val = m1.group(1), m1.group(2)
m2 = re.match(r"\A'(.*)'\Z", val)
if m2:
val = m2.group(1)
m3 = re.match(r'\A"(.*)"\Z', val)
if m3:
val = re.sub(r'\\(.)', r'\1', m3.group(1))
cls.ENVIRON.setdefault(key, str(val))