IPython & 交互式调试速查手册
# Python 开发者必备:IPython & 交互式调试速查手册
# 安装和卸载
pip uninstall ipythonpip install ipython
# 修改 python 版本号
修改脚本第一行指定的 python 链接。 如下
-> % cat /usr/local/bin/ipython
#!$HOME/.pyenv/shims/python
# -*- coding: utf-8 -*-
import re
import sys
from IPython import start_ipython
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(start_ipython())
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 1. 使用 ? 和 ?? 查看方法信息(最常用)
# ? 查看文档说明
d.get?
1
可查看:
- 方法签名
- Docstring(函数说明)
- 所属类
# ?? 查看更详细信息(如果有源码)
d.get??
1
可查看:
- 文档说明
- Python 源码(如果不是 C 实现)
dict是 C 实现的,因此不能看到源码,但能显示更多信息。
# 2. 使用 help() 查看方法说明
help(d.get)
1
进入传统的帮助模式,显示完整文档。退出按 q。
# 3. 使用 inspect 查看函数签名
# 适用于普通函数
import inspect
inspect.signature(d.get)
1
2
3
2
3
输出:
(key, default=None)
1
适用于一切 Python 函数。
# 4. 使用 dir() 查看对象拥有的所有属性/方法
dir(d)
1
可查看:
- 所有可调用方法
- 所有可访问属性
- 魔法方法(
__xxx__)
仅查看列名请用
df.columns;dir()返回的是可用成员名集合。
# 5. 使用 Tab 自动补全(IPython 功能)
d.<Tab>
1
可列出所有可用的属性和方法。
# 6. 查看函数的源码(如果是 Python 编写)
import inspect
inspect.getsource(SomeClass.some_method)
1
2
2
如果是 C 实现的(如内置 dict),会提示无法获取。
# 7. 使用 type() 和 isinstance() 判断对象类型
type(d)
isinstance(d, dict)
1
2
2
# 8. 使用 _、__、___ 获取上一次输出
_
__
___
1
2
3
2
3
分别代表上一行、上两行、上三行的输出。
# 9. 使用 %timeit 测试性能
%timeit d.get("a")
1
仅在 IPython/Jupyter 中可用的 “magic” 命令。
# 10. 使用 ! 执行 Shell 命令
!ls
!pip list
1
2
2
# 11. 常用魔法命令速查
%time/%timeit:单次/多次计时%prun:性能分析(基于 cProfile)%who/%whos:查看当前变量%pwd/%cd/%ls:工作目录与文件%matplotlib inline:在笔记本内渲染图表%debug:在异常后进入 post-mortem 调试%%bash:多行 Bash cell
%whos
%prun func()
1
2
2
# 12. 交互式调试:pdb / ipdb
# 快速断点
import pdb; pdb.set_trace()
# 或 Python 3.7+:
breakpoint()
1
2
3
2
3
常用命令:
n(next)按行执行s(step)步入c(continue)继续到下一个断点l(list)查看源码p expr打印表达式q退出
# IPython 版断点(更友好补全/高亮)
import ipdb; ipdb.set_trace()
1
# 使用 embed() 进行交互式调试
embed() 是 IPython 提供的强大交互式调试工具,允许在代码执行过程中暂停并进入交互式 Python shell。
# 基本用法
from IPython import embed
# 在代码中需要调试的地方插入
embed()
1
2
3
4
2
3
4
# 主要特点
- 可以在代码执行过程中随时暂停
- 提供完整的 Python 交互式环境
- 可以访问当前作用域的所有变量
- 支持 IPython 的所有功能(如自动补全、历史记录等)
# 使用场景
调试复杂逻辑:
def complex_function():
# 一些复杂的计算
result = some_complex_calculation()
# 在关键点插入调试
embed()
# 继续执行
return result
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
检查变量状态:
def process_data(data):
# 处理数据
processed = transform_data(data)
# 检查处理后的数据
embed() # 可以检查 processed 变量的内容
return processed
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 高级用法
自定义提示符:
from IPython import embed
embed(header='调试开始', banner1='当前变量状态:')
1
2
2
条件调试:
def debug_function():
if some_condition:
embed() # 只在特定条件下进入调试模式
1
2
3
2
3
# 退出调试模式
- 输入
exit()或quit() - 按
Ctrl+D(Unix) 或Ctrl+Z(Windows)
# 与 pdb 比较
embed()提供更友好的界面- 支持 IPython 的所有功能
- 更适合交互式调试
注意:不要在生产环境中使用,调试完成后记得删除或注释掉
embed()调用。
# 13. Jupyter 常用技巧
- 在单元格内查看帮助:
func?/func?? - 运行选中行:
Shift+Enter执行当前 cell - 重启内核并全部运行:
Kernel -> Restart & Run All - 自动补全与参数提示:
Tab、Shift+Tab
# 14. 进阶 REPL 工具
rich:更美观的输出与 Tracebackptpython:更强交互式 REPL(多行编辑、补全)
pip install rich ptpython
ptpython
1
2
2
# 参考链接
IPython:https://ipython.readthedocs.io/inspect:https://docs.python.org/3/library/inspect.htmlpdb:https://docs.python.org/3/library/pdb.htmlJupyter:https://jupyter.org/
上次更新: 2025/12/18, 22:17:19