编程语言
首页 > 编程语言> > IPython Notebook中的Stdout与CLI IPython

IPython Notebook中的Stdout与CLI IPython

作者:互联网

从笔记本电脑单元运行时,不显示命令结果.

从IPython笔记本:

使用os.system( “PWD”)
0<-没有错误 从CLI调用的IPython中: 在[15]中:os.system(“ pwd”)
/用户/乔
Out [15]:0<-无错误 我希望从笔记本电脑单元运行命令时会显示/ Users / joe.
缺少了什么?

谢谢,
一世.

解决方法:

解释为here

When you do os.system, it’s not capturing stdout/stderr from the new
process. In the terminal, this works, because stdout and stderr just
go directly to the terminal, without Python ever knowing about them.
In the notebook, it doesn’t, because the kernel can only forward
stdout/stderr that it knows about.

解决该问题的方法是使用子流程:

>>> import subprocess
>>> subprocess.check_output(["pwd"])
/Users/joe

标签:python,ipython,ipython-notebook
来源: https://codeday.me/bug/20191012/1898394.html