使用环境变量启动子进程
Start subprocess with environment variables
我需要启动一个进程,将一些参数传递给环境变量中的子进程。
我知道我可以使用 subprocess.run()
在 python > 3.5 中执行此操作。我怎样才能用 python 2.7 做到这一点?
在python 2.7 我可以修改启动子进程的进程的环境变量。子流程将继承环境变量。但我不想要那样。我只想在子进程中设置环境变量。
使用:subprocess.Popen('env', env={'A':'5','B':'5'})
样本运行:
>>> import subprocess
>>> subprocess.Popen('env', env={'A':'5','B':'5'})
<subprocess.Popen object at 0x102288950>
>>> A=5
B=5
来自docs:
If env is not None, it must be a mapping that defines the environment
variables for the new process; these are used instead of inheriting
the current process’ environment, which is the default behavior.
Python3 subprocess.run()
与 using 相同 Popen
kwarg env
:
The full function signature is largely the same as that of the Popen
constructor
我需要启动一个进程,将一些参数传递给环境变量中的子进程。
我知道我可以使用 subprocess.run()
在 python > 3.5 中执行此操作。我怎样才能用 python 2.7 做到这一点?
在python 2.7 我可以修改启动子进程的进程的环境变量。子流程将继承环境变量。但我不想要那样。我只想在子进程中设置环境变量。
使用:subprocess.Popen('env', env={'A':'5','B':'5'})
样本运行:
>>> import subprocess
>>> subprocess.Popen('env', env={'A':'5','B':'5'})
<subprocess.Popen object at 0x102288950>
>>> A=5
B=5
来自docs:
If env is not None, it must be a mapping that defines the environment variables for the new process; these are used instead of inheriting the current process’ environment, which is the default behavior.
Python3 subprocess.run()
与 using 相同 Popen
kwarg env
:
The full function signature is largely the same as that of the Popen constructor