首页 > 解决方案 > 抑制 *Out[]* IPython 打印,但不抑制其他打印

问题描述

我在 IPyton 中运行一些代码::

jrlab@jrlab-T150s:~$ ipython

In [1]: from IPython import get_ipython
   ...: ipython = get_ipython()
   ...: 
   ...: code = """
   ...: import matplotlib.pyplot as plt
   ...: 
   ...: print("bla")
   ...: 
   ...: plt.figure()
   ...: """
   ...: res = ipython.run_cell(code)
   ...: 
bla
Out[1]: <Figure size 640x480 with 0 Axes>

如何禁用 Out[1] 打印,但不禁用print语句命令的打印?

标签: command-lineipython

解决方案


None不作为输出打印。因此,只需附加; None到您希望抑制其输出的任何行:

In [1]: 1+2                                                                                                                                        
Out[1]: 3

In [2]: 1+2; None                                                                                                                                  

In [3]: 

推荐阅读