1

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.)

Chris Redford
  • 16,982
  • 21
  • 89
  • 109
  • The plumbum (http://plumbum.readthedocs.org) library source might be helpful. – peakxu Apr 20 '14 at 15:08
  • 1
    What does `os.getenv('PATH')` return, both at work and at home, in the python process? – hd1 Apr 20 '14 at 15:14
  • At home, it is the ridiculous `/Library/Frameworks/Python.framework/Versions/Current/bin/ ` listed above. At work, it is the more sensible `/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/software/dist/admin/bin:/opt/www/htdig/bin:/usr/lib/java/bin:/usr/lib/java/jre/bin:/usr/share/texmf/bin` – Chris Redford Apr 20 '14 at 15:16
  • I'm running the Python script from within Sublime Text 2. I think this is relevant because I get a better path at home from the `python` executable running `os.getenv('PATH')`. – Chris Redford Apr 20 '14 at 15:22
  • 1
    Yes. That is it. The command runs fine in the Terminal with `python – Chris Redford Apr 20 '14 at 15:24

1 Answers1

1

I'm leaving this question up because I think the run() command above is useful and I couldn't find anything similar on SO.

However, my solution was very system specific. The problem was, I am running this script within Sublime Text 2 and had manually replaced the path in Python.sublime-settings with

"path": "/Library/Frameworks/Python.framework/Versions/Current/bin/",

I did this because I'd had problems getting ST2 to find the right version of python. Well, this also overwrites the system-wide PATH variable within ST2, thus blocking my access to simple shell programs like ls.

Erasing the "path": ... line from Python.sublime-settings fixed my problem.

EDIT

As suggested by mklement0, changing the line to append the path works as well:

"path": "$PATH:/Library/Frameworks/Python.framework/Versions/Current/bin/",
Community
  • 1
  • 1
Chris Redford
  • 16,982
  • 21
  • 89
  • 109
  • 2
    To _append to_ the existing `$PATH`, use `"path": "$PATH:/Library/Frameworks/Python.framework/Versions/Current/bin/"`. – mklement0 Apr 20 '14 at 15:57