Subprocess.Popen () has inconsistent behavior between Eclipse / PyCharm and terminal execution

The problem I am facing is that Eclipse / PyCharm interprets the results of the Popen () subprocess differently than the standard terminal. Everyone uses python2.6.1 on OSX.

Here is a simple script example:

import subprocess args = ["/usr/bin/which", "git"] print "Will execute %s" % " ".join(args) try: p = subprocess.Popen(["/usr/bin/which", "git"], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # tuple of StdOut, StdErr is the responses, so .. ret = p.communicate() if ret[0] == '' and ret[1] <> '': msg = "cmd %s failed: %s" % (fullcmd, ret[1]) if fail_on_error: raise NameError(msg) except OSError, e: print >>sys.stderr, "Execution failed:", e 

With a standard terminal string:

 ret = p.communicate() 

gives me:

 (Pdb) print ret ('/usr/local/bin/git\n', '') 

Eclipse and PyCharm give me an empty tuple:

 ret = {tuple} ('','') 

Changing the value of shell = value also does not solve the problem. On the terminal, set shell = True and completely pass the command (ie Args = ["/ usr / bin / which git"]) gives me the same result: ret = ('/ usr / local / bin / git \ n' , ''). And Eclipse / PyCharm give me an empty tuple.

Any ideas on what I might be doing wrong?

+7
python eclipse subprocess popen pycharm
source share
1 answer

Well, I found a problem, and this is important to remember when using the IDE in an environment such as Unix. The IDE works in a different context than the terminal user (duh, right ?!). I did not think that the subprocess uses a different environment than the context that I have for my terminal (my terminal has bash_profile to have more things in PATH).

This is easy to verify by modifying the script as follows:

 import subprocess args = ["/usr/bin/which", "git"] print "Current path is %s" % os.path.expandvars("$PATH") try: p = subprocess.Popen(args, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # tuple of StdOut, StdErr is the responses, so .. out, err = p.communicate() if err: msg = "cmd %s failed: %s" % (fullcmd, err) except OSError, e: print >>sys.stderr, "Execution failed:", e 

Under the terminal, the path includes / usr / local / bin. In the IDE, this is not so!

This is an important question for me - always remember the environments!

+14
source share

All Articles