python子进程设置shell变量,然后运行命令-how?

2024-06-08 20:08:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要这样做:

$ export PYRO_HMAC_KEY=123
$ python -m Pyro4.naming

所以,我发现第二个可能与

subprocess.Popen(['python','-m','Pyro4.naming'])

但在此之前如何导出shell变量呢?


Tags: keyexportshellhmacsubprocesspopenpyronaming
3条回答

子流程函数接受一个env参数,该参数可以给定要在流程中使用的环境变量的映射:

subprocess.Popen(['python','-m','Pyro4.naming'], env={'PYRO_HMAC_KEY': '123'})

要更新现有环境。。。

import os, subprocess

d = dict(os.environ)   # Make a copy of the current environment
d['PYRO_HMAC_KEY'] = '123'
subprocess.Popen(['python', '-m', 'Pyro4.naming'], env=d)

相关问题 更多 >