1

I've been scouring forums for hours and can't seem to find a similar issue--could anyone help me understand why subprocess seems to work fine in IDLE but fails when I try to execute a script via the command line?

Here's an example of my code:

import subprocess
subprocess.check_output('notepad', shell=True)

In IDLE, Notepad immediately opens; when I run "python subtest.py" on the command line, I get:

Traceback (most recent call last):
  File "C:\Python27\test\subtest.py", line 1, in <module>
    import subprocess
  File "C:\Python27\test\subprocess.py", line 8, in <module>
    test = subprocess.Popen(my_command, shell=True, stdout=out, stderr=err)
AttributeError: 'module' object has no attribute 'Popen'

It appears to be failing on the import statement--I also tried:

import subprocess
try:
    subprocess.check_output('notepad', shell=True)
except AttributeError as e:
    print e

It fails with the same error--it never gets to the try/except block.

Any suggestions would be most appreciated!

shallcro
  • 11
  • 2
  • 1
    Looks like you have a file named `subprocess.py` on same path where `subtest.py` resides. Try to rename that file. – Abdul Niyas P M Sep 29 '18 at 08:08
  • This can be a pretty baffling and frustrating error if you've never seen it before. – PM 2Ring Sep 29 '18 at 08:17
  • Thank you, @a_python_user! That was the problem! (And here I thought I was clever by trying to use my filenames to keep track of the different things I've been testing...) – shallcro Sep 29 '18 at 08:51
  • Well, that's a logical plan, but as you've learned the hard way, it doesn't work out so well. ;) – PM 2Ring Sep 29 '18 at 09:08

0 Answers0