I have some custom commands.
# works
subprocess.Popen(['python'], stdout=subprocess.PIPE)
But if I have my own system commands like deactivate
, I get that error
Traceback (most recent call last):
File "runner2.py", line 21, in <module>
main()
File "runner2.py", line 18, in main
subprocess.Popen(['deactivate',], stdout=subprocess.PIPE)
File "/usr/lib/python2.6/subprocess.py", line 633, in __init__
errread, errwrite)
File "/usr/lib/python2.6/subprocess.py", line 1139, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
Let alone I need to execute this under my sandbox virtualenv.
Just a note.
shell=True
was likely the correct solution to the o.p., since they did not make the following mistake, but you can also get the "No such file or directory" error if you do not split up your executable from its arguments.Without
shell=True
, Popen expects the executable to be the first element of args, which is why it fails, there is no "echo 1" executable. Addingshell=True
invokes your system shell and passes the first element ofargs
to the shell. i.e. for linux,Popen(['echo 1'], shell=True)
is equivalent toPopen('/bin/sh', '-c', 'echo 1')
which is more overhead than you may need. See Popen() documentation for cases whenshell=True
is actually useful.You have to give the full path to your program
deactivate
and then it the subprocess module should be able to find it.I'm spawning subprocesses like that:
I'm not using subprocess since Python 2.5 does not support it. I had to use use the FULL path to have it working and I guess you also have to use the full path to your custom commands.
Try add an extra parameter 'shell=True' to the
Popen
call.