是否有可能从代码进入ipython?

对于我的debugging需求, pdb是相当不错的。 但是,如果我可以进入ipython ,会更酷(也是有帮助的)。 这可能吗?

有一个ipdb项目将iPythonembedded到标准的pdb中,所以你可以这样做:

 import ipdb; ipdb.set_trace() 

它可以通过普通的pip install ipdb

ipdb是相当短的,所以,而不是easy_installing,你也可以在你的Pythonpath的某处创build一个文件ipdb.py ,并将以下内容粘贴到文件中:

 import sys from IPython.Debugger import Pdb from IPython.Shell import IPShell from IPython import ipapi shell = IPShell(argv=['']) def set_trace(): ip = ipapi.get() def_colors = ip.options.colors Pdb(def_colors).set_trace(sys._getframe().f_back) 

在IPython 0.11中,你可以像这样直接在你的代码中embeddedIPython

你的程序可能看起来像这样

 In [5]: cat > tmpf.py a = 1 from IPython import embed embed() # drop into an IPython session. # Any variables you define or modify here # will not affect program execution c = 2 ^D 

当你运行它时,会发生这种情况(我任意select在现有的ipython会话中运行它,按照我的经验嵌套ipython会话会导致它崩溃)。

 In [6]: In [6]: run tmpf.py Python 2.7.2 (default, Aug 25 2011, 00:06:33) Type "copyright", "credits" or "license" for more information. IPython 0.11 -- 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]: who a embed In [2]: a Out[2]: 1 In [3]: Do you really want to exit ([y]/n)? y In [7]: who ac embed 

相当于

 import pdb; pdb.set_trace() 

与IPython是类似的:

 from IPython.ipapi import make_session; make_session() from IPython.Debugger import Pdb; Pdb().set_trace() 

这有点冗长,但很好知道你是否没有安装ipdb。 make_session调用需要一次来设置颜色scheme等,并且set_trace调用可以放置在任何需要中断的地方。

如果您使用的是更现代版本的IPython(> 0.10.2),则可以使用类似的东西

 from IPython.core.debugger import Pdb Pdb().set_trace() 

但是使用ipdb可能会更好

通常,当我使用ipython的时候,我会用里面的“pdb”命令打开自动debugging。

然后在我的脚本所在的目录中使用“run myscript.py”命令运行我的脚本。

如果我得到一个exception,ipython停止debugging器内的程序。 查看magic ipython命令的帮助命令(%magic)

我喜欢简单地将这一行代码粘贴到我想要设置断点的脚本中:

 __import__('IPython').Debugger.Pdb(color_scheme='Linux').set_trace() 

较新的版本可能会使用:

 __import__('IPython').core.debugger.Pdb(color_scheme='Linux').set_trace() 

看起来最近模块已经被打乱了一下。 在IPython 0.13.1下面为我工作

 from IPython.core.debugger import Tracer; breakpoint = Tracer() breakpoint() # <= wherever you want to set the breakpoint 

虽然可惜,但在qtconsole中这一切都毫无价值 。

较新版本的IPython为将IPython会话embedded和embedded任何Python程序提供了一种简单的机制。 你可以按照下面的配方来embeddedIPython会话:

 try: get_ipython except NameError: banner=exit_msg='' else: banner = '*** Nested interpreter ***' exit_msg = '*** Back in main IPython ***' # First import the embed function from IPython.frontend.terminal.embed import InteractiveShellEmbed # Now create the IPython shell instance. Put ipshell() anywhere in your code # where you want it to open. ipshell = InteractiveShellEmbed(banner1=banner, exit_msg=exit_msg) 

然后,只要你想放入一个IPython shell,就使用ipshell() 。 这将允许您在代码中embedded(甚至嵌套)IPython解释器。

从IPython文档 :

 import IPython.ipapi namespace = dict( kissa = 15, koira = 16) IPython.ipapi.launch_new_instance(namespace) 

将以编程方式启动IPython shell。 显然, namespace字典中的值只是虚拟值 – 在实践中使用locals()可能更有意义。

请注意,您必须对此进行硬编码; 它不会像pdb那样工作。 如果这就是你想要的,DoxaLogos的答案可能更像你正在寻找的东西。

快速简单的方法:

 from IPython.Debugger import Tracer debug = Tracer() 

那就写吧

 debug() 

无论你想开始debugging你的程序。

我不得不谷歌这一对夫妇,如果过去几天,所以添加一个答案…有时很高兴能够正常运行一个脚本,只需放入ipython / ipdb的错误,而不必把ipdb.set_trace()断点进入代码

 ipython --pdb -c "%run path/to/my/script.py --with-args here" 

(首先pip install ipythonpip install ipdb当然)