[英]How do I update the modules that come with python?
I am having trouble with the subprocess module. 我在子流程模块上遇到了麻烦。 I am missing the check_output function and I was wondering if there is a way to update/replace this without doing a complete reinstall of python.
我缺少check_output函数,我想知道是否有一种无需完全重新安装python即可更新/替换它的方法。
Yes, it's possible, you can add the function in yourself if necessary (only suggested if you need it for backwards capability). 是的,有可能,您可以在必要时在自己中添加功能(仅在向后功能需要时才建议使用)。
if 'check_output' not in dir(subprocess):
def check_output(cmd_args, *args, **kwargs):
proc = subprocess.Popen(
cmd_args, *args,
stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs)
out, err = proc.communicate()
if proc.returncode != 0:
raise subprocess.CalledProcessError(args)
return out
subprocess.check_output = check_output
But as that code shows, you can also just write it a little more verbosely and it doesn't operate any differently. 但是,如该代码所示,您也可以稍微冗长一些,并且操作没有任何不同。
Edit: Copy directly from subprocess module version Python 2.7 编辑:直接从子流程模块版本python 2.7复制
def check_output(*popenargs, **kwargs):
if 'stdout' in kwargs:
raise ValueError('stdout argument not allowed, it will be overridden.')
process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
raise subprocess.CalledProcessError(retcode, cmd, output=output)
return output
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.