Python子进程没有属性“check_output”

Cal*_*lus 4 python subprocess python-2.7

我的代码使用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'
Run Code Online (Sandbox Code Playgroud)

请注意,我使用的是 Python2.7,根据这里的这篇文章应该可以解决这个问题,但事实并非如此。

subprocess.check_output() 模块对象有属性“check_output”

是什么赋予了?我尝试了 pip installsubprocess或 pip uninstallsubprocess但没有运气。如何更新subprocess到最新版本以使其具有该check_output属性?我不想使用Popen.

Mar*_*erc 5

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

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