设置/读取环境变量 [英] Setting/reading environment variables
问题描述
可能重复:
如何在Python中设置环境变量
我的python脚本它调用了许多python函数和shell脚本。我想在Python(主调用函数)和所有的子进程(包括shell脚本)中设置环境变量来查看环境变量集。
我需要设置一些环境变量如下:
DEBUSSY 1
pre>
FSDB 1
1
是一个数字,而不是字符串。另外,如何读取存储在环境变量中的值? (如另一个python子脚本中的DEBUSSY
/FSDB
)解决方案尝试使用
os
模块。import os
os.environ ['DEBUSSY'] ='1'
os.environ ['FSDB'] ='1'
#通过os.system(),popen()或fork()和execv()
someVariable = int(os.environ ['DEBUSSY'])打开子进程
请参阅 os.environ 中的rel =noreferrer> Python文档。另外,为了生成子进程,请参阅Python的子进程文档。
Possible Duplicate:
How to set environmental variables in PythonMy python script which calls many python functions and shell scripts. I want to set a environment variable in Python (main calling function) and all the daughter processes including the shell scripts to see the environmental variable set.
I need to set some environmental variables like this:
DEBUSSY 1 FSDB 1
1
is a number, not a string. Additionally, how can I read the value stored in an environment variable? (LikeDEBUSSY
/FSDB
in another python child script.)解决方案Try using the
os
module.import os os.environ['DEBUSSY'] = '1' os.environ['FSDB'] = '1' # Open child processes via os.system(), popen() or fork() and execv() someVariable = int(os.environ['DEBUSSY'])
See the Python docs on
os.environ
. Also, for spawning child processes, see Python's subprocess docs.这篇关于设置/读取环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!