我需要这样做:python subprocess set shell var。然后运行命令 - 如何?
$ export PYRO_HMAC_KEY=123
$ python -m Pyro4.naming
所以,我发现,第二个是有可能与之前
subprocess.Popen(['python','-m','Pyro4.naming'])
但如何导出Shell变量呢?
我需要这样做:python subprocess set shell var。然后运行命令 - 如何?
$ export PYRO_HMAC_KEY=123
$ python -m Pyro4.naming
所以,我发现,第二个是有可能与之前
subprocess.Popen(['python','-m','Pyro4.naming'])
但如何导出Shell变量呢?
要更新现有的环境...
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 = dict(os.environ,PYRO_HMAC_KEY ='123')' – jfs 2015-08-19 00:49:46
子流程功能接受env
论点,即可以给出环境变量的映射的过程中的使用方法:
subprocess.Popen(['python','-m','Pyro4.naming'], env={'PYRO_HMAC_KEY': '123'})
这使得子进程环境除'PYRO_HMAC_KEY'之外为空。 – chepner 2012-08-06 19:46:14
@ F.J非常感谢! – scythargon 2012-08-06 21:50:05
尝试os.putenv():http://docs.python.org/library/os.html#os.putenv
您是否有充足的理由从shell切换到Python?如果shell脚本起作用,可以很容易地从Python中调用它。 – Lenna 2012-08-06 20:47:18
是的,我不想创建像'run_me.sh'这样的附加文件,它不是那么...规范的方式:) – scythargon 2012-08-06 21:48:58