Calculus
Calculus

Reputation: 781

Python subprocess has no attribute "check_output"

I have code that uses subprocess.check_output just fine, I had to reinstall Ubuntu 16.04 and afterwards it's complaining that it now can't find the attribute 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'

Note that I am using Python2.7 which according to this post here should resolve this problem but it doesn't.

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

What gives? I tried pip install subprocess or pip uninstall subprocess but no luck. How do I update subprocess to the latest version so that it has the check_output attribute? I don't want to use Popen.

Upvotes: 5

Views: 3203

Answers (1)

Marjan Moderc
Marjan Moderc

Reputation: 2859

Your problem is that you have created a script, called subprocess.py, so the original library subprocess was "overridden" by your module.

Rename your script /home/username/subprocess.py to something that is not the name of the standard Python module! This rule of course applies to all the other Python libraries!

Upvotes: 7

Related Questions