简体   繁体   English

python子进程设置shell var。 然后运行命令 - 如何?

[英]python subprocess set shell var. and then run command - how?

I need to do this: 我需要这样做:

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

So, i found that the second one is possible to do with 所以,我发现第二个可以做到

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

but how export shell variable before that? 但是之前如何导出shell变量呢?

To update the existing environment... 要更新现有环境......

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)

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

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

尝试os.putenv(): http ://docs.python.org/library/os.html#os.putenv

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

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