subprocess.Popen () hat inkonsistentes Verhalten zwischen Eclipse / PyCharm und Terminalausführung
Das Problem ist, dass Eclipse / PyCharm die Ergebnisse von Popen () eines Unterprozesses anders interpretiert als ein Standardterminal. Alle verwenden unter OSX python2.6.1.
Hier ist ein einfaches Beispielskript:
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
Mit einem Standardterminal lautet die Zeile:
ret = p.communicate()
gibt mir
(Pdb) print ret
('/usr/local/bin/git\n', '')
Eclipse und PyCharm geben mir ein leeres Tupel:
ret = {tuple} ('','')
Changing the shell = value löst das Problem auch nicht. Wenn Sie auf dem Terminal shell = True setzen und den Befehl insgesamt übergeben (dh args = ["/ usr / bin / which git"]), erhalte ich das gleiche Ergebnis: ret = ('/ usr / local / bin / git \ n ',' '). Und Eclipse / PyCharm geben mir beide ein leeres Tupel.
Haben Sie eine Idee, was ich falsch machen könnte?