来自子进程的ipython笔记本输出

Siy*_*Ren 7 python ipython ipython-notebook

使用ipython notebook时,生成的子进程的输出subprocess永远不会出现在笔记本本身中.例如,这个单元格

import subprocess
subprocess.check_call(['echo', 'hello'])
Run Code Online (Sandbox Code Playgroud)

仅显示0为输出,并hello打印在启动ipython的终端上.

是否有任何配置参数我可以调整,以便子进程的输出显示在笔记本本身?


实际上,自定义python c扩展也会吞下它们的输出.有什么问题吗?

Fre*_*ell 6

from subprocess import Popen, PIPE
p = Popen (['echo', 'hello'], stdout=PIPE)
out = p.communicate ()
print (out)
(b'hello\n', None)
Run Code Online (Sandbox Code Playgroud)

您也可以类似地查看stderr


Ami*_*mit 5

check_output如果要捕获输出,请使用.check_call返回退出代码.

import subprocess
print subprocess.check_output(['echo', 'hello'])
Run Code Online (Sandbox Code Playgroud)