Benjamin Mull
Benjamin Mull

Reputation: 21

error in subprocess.Popen vs os.system

I am trying to use subprocess.Popen rather than os.system, but am having a problem. This seems so simple, that I am probably missing something obvious. I am trying to call and run a script I have previously programmed in Octave (the Octave code works fine, and i do not want to have to spend the time to re-write this in NumPy, etc.). I also do not want to keep Octave installed on this particular machine permanently.

This code works perfectly:

os.system('sudo apt-get install octave')
os.system('octave Documents/small_eig.m')
os.system('sudo apt-get remove octave')

This code, however:

subprocess.Popen('sudo apt-get install octave')
subprocess.Popen('octave Documents/small_eig.m')
subprocess.Popen('sudo apt-get remove octave')

Generates the following message:

Traceback (most recent call last):
  File "Documents/ben.py", line 11, in <module>
    subprocess.Popen('sudo apt-get install octave')
  File "/usr/lib/python2.7/subprocess.py", line 709, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1326, in _execute_child
     raise child_exception
OSError: [Errno 2] No such file or directory

Any thoughts?

Thanks,

Upvotes: 1

Views: 856

Answers (1)

shx2
shx2

Reputation: 64328

You need to run the command in "shell-mode". To do that, pass shell=True when calling Popen. Try:

subprocess.Popen('sudo apt-get install octave', shell=True)

Upvotes: 2

Related Questions