简体   繁体   English

设置环境变量时出错

[英]Error setting environment variables

I am working on a Django application. 我正在开发Django应用程序。 I want to put the SECRET_KEY in environment variables. 我想将SECRET_KEY放在环境变量中。 I added this to the .bashrc file: 我将此添加到.bashrc文件中:

export SECRET_KEY=sdfsjhsuresfsdf

The i did source .bashrc 我没有source .bashrc

I am able to access the environment variable from python shell using: 我可以使用以下命令从python shell访问环境变量:

import os
os.environ['SECRET_KEY']

But, it shows a keyError when I try to access this from settings.py file. 但是,当我尝试从settings.py文件访问此文件时,它显示了keyError What am I doing wrong? 我究竟做错了什么?

Try: 尝试:

export SECRET_KEY=sdfsjhsuresfsdf

Then re-source. 然后重新获取资源。 And start a new Python instance. 并启动一个新的Python实例。 It should then be visible. 然后它应该是可见的。

Bash doesn't always auto-export its variables. Bash并不总是自动导出其变量。

In general, OS environment variables are a hard place to put configuration information reliably, because of such issues. 通常,由于这些问题,OS环境变量很难可靠地放置配置信息。 It's hard to track down why variables are seen or not seen. 很难追踪为什么看到或不看到变量。 Reading configuration information out of configuration files (whether .ini, .json, or some other format) is a bit more robust. 从配置文件(无论是.ini,.json还是其他格式)中读取配置信息会更可靠。 But, environment variables are very commonly used, so, when in Rome... 但是,环境变量非常常用,所以在罗马时...

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

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