windows 如何从子流程中获取环境?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1214496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get environment from a subprocess?
提问by Jason R. Coombs
I want to call a process via a python program, however, this process need some specific environment variables that are set by another process. How can I get the first process environment variables to pass them to the second?
我想通过python程序调用一个进程,但是,这个进程需要一些由另一个进程设置的特定环境变量。如何获取第一个进程环境变量以将它们传递给第二个?
This is what the program look like:
这是程序的样子:
import subprocess
subprocess.call(['proc1']) # this set env. variables for proc2
subprocess.call(['proc2']) # this must have env. variables set by proc1 to work
but the to process don't share the same environment. Note that these programs aren't mine (the first is big and ugly .bat file and the second a proprietary soft) so I can't modify them (ok, I can extract all that I need from the .bat but it's very combersome).
但是 to 进程不共享相同的环境。请注意,这些程序不是我的(第一个是又大又丑的 .bat 文件,第二个是专有软件)所以我无法修改它们(好吧,我可以从 .bat 中提取我需要的所有内容,但它非常麻烦)。
N.B.: I am using Windows, but I prefer a cross-platform solution (but my problem wouldn't happen on a Unix-like ...)
注意:我使用的是 Windows,但我更喜欢跨平台解决方案(但我的问题不会发生在类 Unix 上......)
采纳答案by Glenn Maynard
Since you're apparently in Windows, you need a Windows answer.
由于您显然在 Windows 中,因此您需要一个 Windows 答案。
Create a wrapper batch file, eg. "run_program.bat", and run both programs:
创建一个包装批处理文件,例如。“run_program.bat”,并运行两个程序:
@echo off
call proc1.bat
proc2
The script will run and set its environment variables. Both scripts run in the same interpreter (cmd.exe instance), so the variables prog1.bat sets willbe set when prog2 is executed.
该脚本将运行并设置其环境变量。两个脚本都在同一个解释器(cmd.exe 实例)中运行,因此在执行 prog2 时将设置变量 prog1.bat 设置。
Not terribly pretty, but it'll work.
不是很漂亮,但它会起作用。
(Unix people, you can do the same thing in a bash script: "source file.sh".)
(Unix 人,你可以在 bash 脚本中做同样的事情:“源文件.sh”。)
回答by Jason R. Coombs
Here's an example of how you can extract environment variables from a batch or cmd file without creating a wrapper script. Enjoy.
下面是一个示例,说明如何在不创建包装器脚本的情况下从批处理或 cmd 文件中提取环境变量。享受。
from __future__ import print_function
import sys
import subprocess
import itertools
def validate_pair(ob):
try:
if not (len(ob) == 2):
print("Unexpected result:", ob, file=sys.stderr)
raise ValueError
except:
return False
return True
def consume(iter):
try:
while True: next(iter)
except StopIteration:
pass
def get_environment_from_batch_command(env_cmd, initial=None):
"""
Take a command (either a single command or list of arguments)
and return the environment created after running that command.
Note that if the command must be a batch file or .cmd file, or the
changes to the environment will not be captured.
If initial is supplied, it is used as the initial environment passed
to the child process.
"""
if not isinstance(env_cmd, (list, tuple)):
env_cmd = [env_cmd]
# construct the command that will alter the environment
env_cmd = subprocess.list2cmdline(env_cmd)
# create a tag so we can tell in the output when the proc is done
tag = 'Done running command'
# construct a cmd.exe command to do accomplish this
cmd = 'cmd.exe /s /c "{env_cmd} && echo "{tag}" && set"'.format(**vars())
# launch the process
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, env=initial)
# parse the output sent to stdout
lines = proc.stdout
# consume whatever output occurs until the tag is reached
consume(itertools.takewhile(lambda l: tag not in l, lines))
# define a way to handle each KEY=VALUE line
handle_line = lambda l: l.rstrip().split('=',1)
# parse key/values into pairs
pairs = map(handle_line, lines)
# make sure the pairs are valid
valid_pairs = filter(validate_pair, pairs)
# construct a dictionary of the pairs
result = dict(valid_pairs)
# let the process finish
proc.communicate()
return result
So to answer your question, you would create a .py file that does the following:
因此,要回答您的问题,您将创建一个执行以下操作的 .py 文件:
env = get_environment_from_batch_command('proc1')
subprocess.Popen('proc2', env=env)
回答by Martin v. L?wis
As you say, processes don't share the environment - so what you literally ask is not possible, not only in Python, but with any programming language.
正如你所说,进程不共享环境 - 所以你真正问的是不可能的,不仅在 Python 中,而且在任何编程语言中都是如此。
What you cando is to put the environment variables in a file, or in a pipe, and either
您可以做的是将环境变量放在文件或管道中,然后
- have the parent process read them, and pass them to proc2 before proc2 is created, or
- have proc2 read them, and set them locally
- 让父进程读取它们,并在创建 proc2 之前将它们传递给 proc2,或者
- 让 proc2 读取它们,并在本地设置它们
The latter would require cooperation from proc2; the former requires that the variables become known before proc2 is started.
后者需要 proc2 的合作;前者要求在启动 proc2 之前知道变量。
回答by thomas
The Python standard module multiprocessinghave a Queues system that allow you to pass pickle-able object to be passed through processes. Also processes can exchange messages (a pickled object) using os.pipe. Remember that resources (e.g : database connection) and handle (e.g : file handles) can't be pickled.
Python 标准模块 multiprocessing有一个 Queues 系统,允许您传递可通过进程传递的pickle对象。进程也可以使用 os.pipe 交换消息(腌制对象)。请记住,资源(例如:数据库连接)和句柄(例如:文件句柄)不能被腌制。
You may find this link interesting : Communication between processes with multiprocessing
您可能会发现此链接很有趣: 具有多处理的进程之间的通信
Also the PyMOTw about multiprocessing worth mentioning : multiprocessing Basics
还有关于多处理的 PyMOTw 值得一提: 多处理基础
sorry for my spelling
对不起我的拼写
回答by David Z
Two things spring to mind: (1) make the processes share the same environment, by combining them somehow into the same process, or (2) have the first process produce output that contains the relevant environment variables, that way Python can read it and construct the environment for the second process. I think (though I'm not 100% sure) that there isn't any way to get the environment from a subprocess as you're hoping to do.
想到两件事:(1)使进程共享相同的环境,通过以某种方式将它们组合到同一个进程中,或者(2)让第一个进程产生包含相关环境变量的输出,这样 Python 可以读取它和为第二个过程构建环境。我认为(尽管我不是 100% 确定)没有任何方法可以像您希望的那样从子流程中获取环境。
回答by William Pursell
Environment is inherited from the parent process. Set the environment you need in the main script, not a subprocess (child).
环境继承自父进程。在主脚本中设置您需要的环境,而不是子进程(子进程)。