对于我的调试需求,pdb
是相当不错的。然而,如果我能进入ipython
,那将会更酷(也更有帮助)。这是可能的吗?
发布于 2009-07-14 18:18:54
有一个ipdb
项目将iPython嵌入到标准的pdb中,所以你可以这样做:
import ipdb; ipdb.set_trace()
它可以通过通常的pip install ipdb
安装。
ipdb
非常短,所以您也可以在easy_installing路径上的某个位置创建一个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)
发布于 2011-09-14 13:01:00
在IPython 0.11中,可以将IPython直接嵌入到代码中,如下所示
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
这就是运行它时发生的事情(我随意选择在现有的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
a c embed
发布于 2012-09-21 18:41:44
如果您使用的是较新版本的IPython (> 0.10.2),则可以使用以下内容
from IPython.core.debugger import Pdb
Pdb().set_trace()
但是使用ipdb可能会更好
https://stackoverflow.com/questions/1126930
复制相似问题