收到错误-AttributeError:运行subprocess.run([[“ ls”,“ -l”])时'module'对象没有属性'run' [英] Getting an error - AttributeError: 'module' object has no attribute 'run' while running subprocess.run(["ls", "-l"])
本文介绍了收到错误-AttributeError:运行subprocess.run([[“ ls”,“ -l”])时'module'对象没有属性'run'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在AIX 6.1上运行并使用Python 2.7。想要执行以下行但出现错误。
I am running on a AIX 6.1 and using Python 2.7. Want to execute following line but getting an error.
subprocess.run(["ls", "-l"])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'run'
推荐答案
subprocess.run()
函数仅在Python 3.5和更高版本中存在。
The subprocess.run()
function only exists in Python 3.5 and newer.
但是很容易向后移植:
def run(*popenargs, **kwargs):
input = kwargs.pop("input", None)
check = kwargs.pop("handle", False)
if input is not None:
if 'stdin' in kwargs:
raise ValueError('stdin and input arguments may not both be used.')
kwargs['stdin'] = subprocess.PIPE
process = subprocess.Popen(*popenargs, **kwargs)
try:
stdout, stderr = process.communicate(input)
except:
process.kill()
process.wait()
raise
retcode = process.poll()
if check and retcode:
raise subprocess.CalledProcessError(
retcode, process.args, output=stdout, stderr=stderr)
return retcode, stdout, stderr
不支持超时,也没有用于完成过程信息的自定义类,所以我只返回 retcode
, stdout
和 stderr
信息。否则,它的功能与原始功能相同。
There is no support for timeouts, and no custom class for completed process info, so I'm only returning the retcode
, stdout
and stderr
information. Otherwise it does the same thing as the original.
这篇关于收到错误-AttributeError:运行subprocess.run([[“ ls”,“ -l”])时'module'对象没有属性'run'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文