Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I'm executing a line of shell script using python. It isn't working with which to test of the existence of homebrew

#!/usr/bin/python
import sys, subprocess
subprocess.call(["which python"])

throws the long error

Traceback (most recent call last):
  File "installations.py", line 5, in <module>
    subprocess.call(["which python"]) 
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 522, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 709, in __init__
    errread, errwrite)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1326, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

But I know that the shell execution is working correctly on some level

#!/usr/bin/python
import sys, subprocess
subprocess.call(["whoami]) 

prints my username just fine. Am I doing something wrong, or is which for some reason not supported. Is there some better supported way of detecting the existence of an installation?

share|improve this question
up vote 3 down vote accepted

The failing call is trying to find an executable named 'which python' not running which with an argument of python as you likely intended. The list that you pass to call (unless shell=True is set) is the list of the command and all the arguments. Doing

subprocess.call(['which', 'python'])

will probably give you what you are looking for.

share|improve this answer
    
Perfect. So is there something better for executing strings of shell script? Something like PHP's shell_exec()? Thanks so much! – 1252748 May 5 '15 at 16:43
    
The subprocess module is the "best" way to execute commands from python generally. If you want it to look more like a shell command as you'd type it, and probably is more like PHP's shell_exec you can add the argument shell=True to your call() function, then the first argument becomes a string to exec at a shell instead of the executable and argument list as a lilst, so you could do subprocess.call( "which python", shell=True) for this example if you wanted to--but then you have to deal with lots more quote escaping and stuff like that – Eric Renouf May 5 '15 at 17:03

When calling this way you need to separate each word on your command line as a different element of an iterable:

subprocess.call(["which", "python"])

I highly recommend reading the docs several times through in order to truly understand all the possible ways of launching subprocesses and how they differ from each other.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.