如何在Python中设置环境变量 [英] How to set environment variables in Python
问题描述
我需要在python脚本中设置一些环境变量,并希望从python(shell脚本)中调用的所有其他脚本,这些脚本将作为子进程来查看环境变量集。该值是一个数字。
I need to set some environment variables in the python script and I want all the other scripts that are called from python (shell scripts) which will be child process to see the environment variables set. The value is a number.
如果我做 os.environ [DEBUSSY] = 1
,它抱怨说 1
必须是字符串。
一旦我设置它,我也想知道如何读取python中的环境变量(在脚本的后面部分)。
If I do os.environ["DEBUSSY"] = 1
, it complains saying that 1
has to be string.
I also want to know how to read the environment variables in python (in the later part of the script) once I set it.
推荐答案
环境变量必须是字符串,所以使用
Environment variables must be strings, so use
os.environ["DEBUSSY"] = "1"
设置变量 DEBUSSY
到字符串 1
。要稍后访问此变量,只需使用
to set the variable DEBUSSY
to the string 1
. To access this variable later, simply use
print os.environ["DEBUSSY"]
子进程自动继承父进程的环境 - 不需要您的特殊操作。
Child processes automatically inherit the environment of the parent process -- no special action on your part is required.
这篇关于如何在Python中设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!