2017-05-09 69 views
0

我使用python中的子流程来捕捉来自bazel应用tensorflow的结果。Python子流程输出

import subprocess 
cmd = ["bazel-bin/tensorflow/examples/label_image/label_image"] 
pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) 
output = pipe.communicate()[0] 
print "Result: ", output 

的问题是,结果显示在终端,我无法捕捉到它的变量“输出”

,并返回结果:(无)

+4

尝试使用'stderr = subprocess.PIPE'和'output,err = pipe.communicate()'来捕获stderr并查看错误信息。 – sberry

+2

正如@sberry所说 - 输出可能会发送到'stderr' ...另外,当使用'subprocess'模块时,你可以在'Popen'周围使用'check_output'包装器,例如:'output = subprocess.check_output(cmd ,stderr = subprocess.PIPE,shell = True)' –

+1

另外,这里不需要'shell = True'。该命令位于列表中,可以在不通过子shell的情况下执行。 – tdelaney

回答

0

解决的评论:

Try also capturing stderr with stderr=subprocess.PIPE and output, err = pipe.communicate() and see what is in err. – sberry