I'm trying to make a simple command that will let me run bash fully in Python, including output strings.
This function worked great on systems I use at my job:
import subprocess
def run(command):
output = subprocess.check_output(command, shell=True)
return output
However, now I'm using it at home and the PATH variable doesn't match the one in my Terminal.
So when I execute
run('ls')
I get this:
/bin/sh: ls: command not found
Which makes sense because, nonsensically, the PATH I get from running
print run('/usr/bin/env')
is
PATH=/Library/Frameworks/Python.framework/Versions/Current/bin/
Now, I could remedy all this by using:
run('/bin/ls')
But that defeats the entire purpose of using this command, which is to faithfully emulate the bash
shell.
How do I make run() use the system's PATH or create an equivalent function that just works?
(No platitudes about the dangers of using 'shell=True'
, please. This is all personal use with innocuous commands like ls
and ps axw
.)