【问题标题】:Python subprocess has no attribute "check_output"Python子进程没有属性“check_output”
【发布时间】:2018-02-21 03:11:18
【问题描述】:

我的代码使用 subprocess.check_output 很好,我不得不重新安装 Ubuntu 16.04,然后它抱怨它现在找不到属性 check_output。

import subprocess

p = subprocess.check_output("here is a command", shell=True)

/usr/bin/python2.7 /home/username/subprocess.py
Traceback (most recent call last):
  File "/home/username/subprocess.py", line 1, in <module>
    import subprocess
  File "/home/username/subprocess.py", line 4, in <module>
    p = subprocess.check_output("here is a command", shell=True)
AttributeError: 'module' object has no attribute 'check_output'

请注意,我使用的是 Python2.7,根据这里的这篇文章应该可以解决这个问题,但它没有。

subprocess.check_output() module object has out attribute 'check_output'

什么给了?我试过 pip install subprocess 或 pip uninstall subprocess 但没有运气。如何将subprocess 更新到最新版本,使其具有check_output 属性?我不想使用Popen

【问题讨论】:

  • 你能告诉我们你是如何确定你正在使用 Python2.7 的吗?你能提供一些在交互式 shell 中运行相同步骤的输出吗?

标签: python python-2.7 subprocess


【解决方案1】:

您的问题是您创建了一个名为subprocess.py 的脚本,因此原始库subprocess 被您的模块“覆盖”了。

将您的脚本 /home/username/subprocess.py 重命名为不是标准 Python 模块的名称!这条规则当然适用于所有其他 Python 库!

【讨论】:

  • 哇,你说得对,我只是忽略了我将文件命名为 subprocess.py 的事实。多么蹩脚的问题:(
  • 这只是 python 需要如何修复包与相对导入的另一个示例。
猜你喜欢
  • 1970-01-01
  • 2016-07-10
  • 2019-05-30
  • 2018-03-10
  • 1970-01-01
  • 1970-01-01
  • 2021-12-12
  • 2010-10-21
  • 1970-01-01
相关资源
最近更新 更多