IPython 嵌入断点 - 如何查看断点周围的代码?

问题描述 投票:0回答:1

假设我有一些带有

embed
断点的 Python 代码,如下所示:

from IPython import embed

def my_func(self):
  some_var = 'awesome sauce'
  embed()
  # other stuff happens

当我运行我的测试运行程序时...说

pytest
:

$ python -m pytest -k test_something_awesome

我的 ipython 终端如何为我提供嵌入断点周围的上下文代码?

所以代替:

enter image description here

我会得到类似于 ruby 的 PRY 输出的内容,例如:

enter image description here

python debugging ipython pry
1个回答
0
投票

您可以使用

inspect
模块在 IPython 中手动执行此操作:

import inspect
stack = inspect.stack(5)                # 5 lines of context
print("".join(stack[11]).code_context)
# For me, entries 0..10 are all IPython internals.
© www.soinside.com 2019 - 2024. All rights reserved.