Getting an error - AttributeError: #39;module#39; object has no attribute #39;run#39; while running subprocess.run([quot;lsquot;, quot;-lquot;])(出现错误 - AttributeError: module object has no attribute run while running subprocess.run([ls, -l]))
问题描述
我在 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.
然而,向后移植很容易:
It is easy enough to backport however:
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: 'module' object has no attribute 'run' while running subprocess.run(["ls", "-l"])的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:出现错误 - AttributeError: 'module' object has no attribute 'run' while running subprocess.run(["ls", "-l"])
基础教程推荐
- Kivy 使用 opencv.调整图像大小 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
