假设我有一些带有
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 终端如何为我提供嵌入断点周围的上下文代码?
所以代替:
我会得到类似于 ruby 的 PRY 输出的内容,例如:
您可以使用
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.