IPython启用pdb
项目描述
IPython pdb
使用
ipdb导出功能以访问IPython调试器,该调试器具有Tab完成、语法高亮、更好的回溯和与
示例使用
import ipdb
ipdb.set_trace()
ipdb.set_trace(context=5) # will show five lines of code
# instead of the default three lines
# or you can set it via IPDB_CONTEXT_SIZE env variable
# or setup.cfg file
ipdb.pm()
ipdb.run('x[0] = 3')
result = ipdb.runcall(function, arg0, arg1, kwarg='foo')
result = ipdb.runeval('f(1,2) - 3')
set_trace的参数
set_trace函数接受
context
,它将显示定义的代码行数,以及cond
,它接受布尔值(例如abc == 17
)并且当cond
等于True
时将启动ipdb的界面。使用配置文件
您可以在主目录下使用
.ipdb
文件,或在项目目录下使用setup.cfg
或pyproject.toml
来设置上下文。您也可以通过环境变量$IPDB_CONFIG
设置您的文件位置。您的环境变量优先于主配置文件,而主配置文件又优先于设置配置文件。目前,仅支持上下文设置。一个有效的setup.cfg如下所示
[ipdb] context=5
一个有效的.ipdb如下所示
context=5
有效的pyproject.toml示例如下
[tool.ipdb] context=5
事后分析函数,ipdb.pm(),等同于魔法函数%debug。
如果您使用支持setuptools入口点的工具安装ipdb,则会为您创建一个ipdb脚本。您可以使用它来调试您的Python 2脚本,如下所示
$ bin/ipdb mymodule.py
对于Python 3
$ bin/ipdb3 mymodule.py
或者,仅在Python 2.7中,您还可以使用
$ python -m ipdb mymodule.py
您还可以使用with语句将代码括起来,以在抛出异常时启动ipdb
from ipdb import launch_ipdb_on_exception
with launch_ipdb_on_exception():
[...]
或者,您可以使用iex作为函数装饰器,在抛出异常时启动ipdb
from ipdb import iex
@iex
def main():
[...]
stdout问题
一些工具,如nose,会篡改stdout。
直到ipdb==0.9.4,我们尝试猜测何时也应该篡改stdout以支持这些工具。然而,直到0.9.4尝试的所有策略都已被证明是不稳定的。
如果您使用nose或另一个篡改stdout的工具,您应该显式地使用以下方式请求stdout篡改
import ipdb
ipdb.sset_trace()
ipdb.spm()
from ipdb import slaunch_ipdb_on_exception
with slaunch_ipdb_on_exception():
[...]
开发
ipdb源代码和跟踪器位于https://github.com/gotcha/ipdb。
请务必在HISTORY.txt中更新更改日志。
在未发布部分,添加您的更改和您的用户名。
手动测试
要测试您的更改,请使用manual_test.py。创建一个虚拟环境,安装IPython并运行python manual_test.py,检查您的更改是否生效。如果可能,创建自动化测试以更好地控制行为。
自动化测试
要本地运行自动化测试,请创建一个虚拟环境,安装coverage并运行coverage run setup.py test。
第三方支持
Products.PDBDebugMode
Zope2 Products.PDBDebugMode如果可用,会使用ipdb代替pdb。
iw.debug
iw.debug允许您在任何Zope2应用程序的已发布对象上触发ipdb调试器。
ipdbplugin
ipdbplugin是一个nose测试运行器插件,它也使用IPython调试器而不是pdb。(它不再依赖于ipdb了)。
变更日志
0.13.13 (2023-03-09)
使用上下文管理器打开toml配置[andrewzwicky]
0.13.12 (2023-03-09)
修改测试以单独测试3.11与其他早期Python版本。[andrewzwicky]
0.13.11 (2022-12-13)
查找配置时的异常处理更好。[bignose-debian]
0.13.10 (2022-12-13)
更好的toml支持(使用tomllib对3.11,使用tomli对3.6至3.10)。[salty-horse, mgorny]
最小的PEP 517支持。[cpcloud]
支持以模块和脚本方式运行3.11。[nphilipp, gotcha]
基于OSV:PYSEC-2022-12更改ipython依赖关系,对于使用Python 3.6的用户,安装7.16.3 <= IPython < 7.17.0,对于使用Python > 3.6的用户,安装IPython >= 7.31.1。[malkstar]
0.13.9 (2021-06-02)
当 pyproject.toml 文件不包含 tool 部分时再次修复。 [markab108]
0.13.8 (2021-05-26)
当 pyproject.toml 文件不包含 tool 部分时修复。 [anjos]
添加便利函数 iex()。 [alanbernstein]
0.13.7 (2021-03-16)
导入时不实例化 IPython。[adamchainz]
0.13.6 (2021-03-08)
修复 pyproject.toml 解析错误。[alexandrebarbaruiva]
0.13.5 (2021-03-02)
添加对 pyproject.toml 作为配置文件的支持。[alexandrebarbaruiva]
对于使用 python 3.4 的用户,安装 6.0.0 <= IPython < 7.0.0;对于使用 python 3.5 的用户,安装 7.0.0 <= IPython < 7.10.0;对于使用 python 3.6 的用户,安装 7.10.0 <= IPython < 7.17.0;对于使用 python >3.6 的用户,安装 IPython >= 7.17.0。[d1618033]
0.13.4 (2020-10-01)
添加 ‘-m’ 选项。[mrmino]
0.13.3 (2020-06-23)
允许 runcall, runeval 也使用设置上下文值。[meowser]
向 set_trace 添加条件参数。[alexandrebarbaruiva]
0.13.2 (2020-03-03)
移除多余的调试代码。[gotcha]
0.13.1 (2020-02-28)
修复没有配置文件时的情况。[gotcha]
0.13.0 (2020-02-28)
添加通过环境变量或配置文件设置上下文的选项。[alexandrebarbaruiva]
0.12.3 (2019-12-03)
修复使用说明中的版本错误。[gotcha]
0.12.2 (2019-07-30)
避免发出 term-title 字节。[steinnes]
0.12.1 (2019-07-26)
修复 –help。[native-api]
0.12 (2019-03-20)
停止支持 Python 3.3.x。[bmw]
当使用 IPython >= 5.1 时停止发出弃用警告。已停止对 IPython < 5.1 的支持。[bmw]
0.11 (2018-02-15)
简化加载 IPython 和从中获取信息。停止支持 python 2.6,停止支持 IPython < 5.0.0。[takluyver]
0.10.3 (2017-04-22)
对于使用 python 2.6 的用户,不要安装 IPython >= 2.0.0。对于使用 python 2.7 的用户,不要安装 IPython >= 6.0.0。[vphilippon]
停止支持 python 3.2。[vphilippon]
命令行使用与 pdb 一致 - 添加命令参数。[zvodd]
0.10.2 (2017-01-25)
询问 IPython 使用哪个调试器类。关闭 https://github.com/gotcha/ipdb/issues/105。[gnebehay, JBKahn]
ipdb.set_trace() 不再忽略上下文参数。关闭 https://github.com/gotcha/ipdb/issues/93。[gnebehay, Garrett-R]
0.10.1 (2016-06-14)
支持 IPython 5.0。[ngoldbaum]
0.10.0 (2016-04-29)
停止尝试在需要时自动猜测何时需要捕获 stdout。例如,由 nose 需要。相反,提供一组在需要时可以显式调用的函数。[gotcha]
停止支持 IPython 0.10.2 之前的版本。
0.9.4 (2016-04-29)
修复使用 python -m ipdb 时的重启错误。关闭 https://github.com/gotcha/ipdb/issues/93。[gotcha]
0.9.3 (2016-04-15)
不需要用户传递 traceback 到 post_mortem。[Wilfred]
0.9.2 (2016-04-15)
关闭 https://github.com/gotcha/ipdb/issues/93。[gotcha]
0.9.1 (2016-04-12)
重置 sys.modules['__main__'] 到原始值。关闭 https://github.com/gotcha/ipdb/issues/85。[gotcha]
修复对 IPython 版本 0.x 的支持。[asivokon]
0.9.0 (2016-02-22)
切换到修订版 BSD 许可证(经所有贡献者批准)。关闭 https://github.com/gotcha/ipdb/issues/68。[lebedov, gotcha]
0.8.3 (2016-02-17)
不要将 sys.argv 传递给 IPython 进行配置。[emulbreh]
0.8.2 (2016-02-15)
修复版本号词法比较。[sas23]
允许通过 set_trace 配置显示代码上下文行数。[JamshedVesuna]
如果已运行 IPython 实例,则加载其配置。[IxDay]
0.8.1 (2015-06-03)
使 Nose 支持不那么侵入式。关闭 https://github.com/gotcha/ipdb/issues/52,关闭 https://github.com/gotcha/ipdb/issues/31。[blink1073, gotcha]
在上下文管理器中修复 post_mortem。关闭 https://github.com/gotcha/ipdb/issues/20。[omergertel]
0.8 (2013-09-19)
更多 Python 3 兼容性;这意味着放弃对 Python 2.5 的支持。[gotcha]
0.7.1 (2013-09-19)
与 IPython 1.0 兼容。[pgularski]
在 setup.py 中对 version_info 进行索引以实现 Python 2.6 兼容性。[kynan]
添加 Travis CI 配置。[kynan]
0.7 (2012-07-06)
添加 launch_ipdb_on_exception 上下文管理器。意味着放弃对 Python 2.4 的支持。[Psycojoker]
仅包装一次 sys.excepthook。[marciomazza]
添加 GPL 文件并在头部引用。[stan3]
支持 Python 3。[Piet Delport]
基本测试。[msabramo]
添加了函数 runcall、runeval 和 run。[dimasad]
0.6.1 (2011-10-17)
IPython 的依赖状态晚于或等于 0.10。[gotcha]
0.6 (2011-09-01)
添加 setuptools console_scripts 入口。[akrito, gotcha]
Nose 支持。解决 https://github.com/gotcha/ipdb/issues/8。[akaihola, gotcha]
0.5 (2011-08-05)
支持 IPython 0.11。[lebedov]
0.4 (2011-06-13)
当从 IPython 使用时,使用其颜色。解决 https://github.com/gotcha/ipdb/issues/1。[gotcha]
修复了退出时使用“q”时的错误。[gotcha]
允许使用 python -m ipdb mymodule.py。仅支持 Python 2.7。解决 https://github.com/gotcha/ipdb/issues/3。[gotcha]
修复了当 traceback 为 None 时 post_mortem 的问题。[maurits]
0.3 (2011-01-16)
为 Products.PDBDebugMode 添加 post_mortem() 支持。[Jean Jordaan]
迁移到 github.com。
0.2 (2010-10-20)
添加 pm()。[Paulo Benedict Ang]
0.1 (2010-04-26)
首次“非开发”版本。
项目详情
下载文件
下载适合您平台的文件。如果您不确定选择哪个,请了解有关 安装包 的更多信息。