如何禁止IPython启动消息?

时间:2016-01-14 20:06:35

标签: python ipython

调试时,我经常会进入IPython shell进行交互式代码测试。但是,这也会导致python版本,日期和帮助指令的大型信息转储到stdout。如何抑制此信息,以免模糊故意调试消息?

x = get_important_data()
print 'important info! right here! The data is in variable "x"'
import IPython
IPython.embed()

此代码提供如下输出......

important info! right here! The data is in variable "x"
Python 2.7.11 |Anaconda 2.4.0 (x86_64)| (default, Dec  6 2015, 18:57:58) 
Type "copyright", "credits" or "license" for more information.

IPython 4.0.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]:

1 个答案:

答案 0 :(得分:4)

你可以这样做:

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 365, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: dlopen(libportmidi.dylib, 6): image not found

IPython.embed(banner1="") 设置为空字符串会使启动消息消失。它实际上不会删除消息,但会用空字符串替换它们。

您还可以使用banner1banner1banner2参数添加有用的消息:

exit_msg

如果您需要从命令行启动IPython实例,可以执行以下操作:

IPython.embed(
    banner1="Entering Debug Mode", 
    banner2="Here's another helpful message",
    exit_msg="Exiting the debug mode!"
)