在Python中读写环境变量? [英] Reading and writing environment variables in Python?
问题描述
我的python脚本,它调用了许多python函数和shell脚本.我想在Python(主要调用函数)中设置一个环境变量,并在所有子进程中(包括shell脚本)设置该变量,以查看环境变量集.
My 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
是数字,而不是字符串.另外,如何读取存储在环境变量中的值? (就像另一个Python子脚本中的DEBUSSY
/FSDB
一样.)
1
is a number, not a string. Additionally, how can I read the value stored in an environment variable? (Like DEBUSSY
/FSDB
in another python child script.)
推荐答案
尝试使用os
模块.
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'])
请参见os.environ
上的 Python文档.另外,有关生成子进程的信息,请参见Python的子进程文档.
See the Python docs on os.environ
. Also, for spawning child processes, see Python's subprocess docs.
这篇关于在Python中读写环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!