python - 如何从 ipdb 中分配给 ipython 全局命名空间?

标签 python ipython jupyter-notebook pdb ipdb

我在使用 %debug 魔法调试的 IPython 笔记本中有一个回溯。我想将回溯框架中的一个对象分配给 IPython 全局命名空间中的一个变量,这样我就可以在 ipdb 提示符(相当笨拙)之外处理它。最简单的方法是什么?

我现在通过 pickle ipdb 中的对象并将其从全局命名空间中取消 pickle 解决了这个问题,但我确信有更好的方法。

最佳答案

一种方法是将值分配给模块成员(类似于模块范围内的全局变量),它在您退出 pdb session 后仍然存在,因为该模块已经在 sys.modules 中,并留在那里。

1% os.path.exists(3254)
...
TypeError: coercing to Unicode: need string or buffer, int found

2% %debug
...
ipdb> os.MYVAR = 234
ipdb> q

3% os.MYVAR
3= 234

关于python - 如何从 ipdb 中分配给 ipython 全局命名空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29661014/

相关文章:

python - 更改 plt.plot 的 for 循环中的标题并创建 6x16 子图

python - 嘘声 : 'PED-RPC> remote method restart_target cannot be found'

python - 导入错误 : cannot import name murmurhash3_32

python - (Python C API)PyRun_StringFlags 缺少内置函数?

python - 加载模块后运行 Python 启动代码

python - list.append 的意外行为

jupyter-notebook - 为 Jupyter Notebook 安装和运行 R 内核?

python - 在 Python 中导航目录

python - Jupyter 不渲染 latex

python - 将 jupyter lab notebook 转换为脚本而不添加注释和单元格之间的新行