使用IPython作为有效的调试器 [英] Using IPython as an effective debugger
问题描述
如何在我的代码中嵌入一个IPython shell并让它自动显示调用它的行号和函数?
How can I embed an IPython shell in my code and have it automatically display the line number and function in which it was invoked?
我目前有以下设置在我的代码中嵌入IPython shell:
I currently have the following setup to embed IPython shells in my code:
from IPython.frontend.terminal.embed import InteractiveShellEmbed
from IPython.config.loader import Config
# Configure the prompt so that I know I am in a nested (embedded) shell
cfg = Config()
prompt_config = cfg.PromptManager
prompt_config.in_template = 'N.In <\\#>: '
prompt_config.in2_template = ' .\\D.: '
prompt_config.out_template = 'N.Out<\\#>: '
# Messages displayed when I drop into and exit the shell.
banner_msg = ("\n**Nested Interpreter:\n"
"Hit Ctrl-D to exit interpreter and continue program.\n"
"Note that if you use %kill_embedded, you can fully deactivate\n"
"This embedded instance so it will never turn on again")
exit_msg = '**Leaving Nested interpreter'
# Put ipshell() anywhere in your code where you want it to open.
ipshell = InteractiveShellEmbed(config=cfg, banner1=banner_msg, exit_msg=exit_msg)
this允许我通过使用 ipshell()
在我的代码中的任何地方启动一个完整的IPython shell。例如,以下代码:
This allows me to start a full IPython shell anywhere in my code by just using ipshell()
. For example, the following code:
a = 2
b = a
ipshell()
在调用者的范围内启动一个IPython shell,允许我检查的值a
和 b
。
starts an IPython shell in the scope of the caller that allows me inspect the values of a
and b
.
我想做的是自动每当我调用 ipshell()
时,运行以下代码:
What I would like to do is to automatically run the following code whenever I call ipshell()
:
frameinfo = getframeinfo(currentframe())
print 'Stopped at: ' + frameinfo.filename + ' ' + str(frameinfo.lineno)
这将始终显示IPython shell启动的上下文,以便我知道正在调试的文件/函数等。
This would always show the context where the IPython shell starts so that I know what file/function, etc. I am debugging.
也许我可以用装饰器做到这一点,但到目前为止我的所有尝试都失败了,因为我需要 ipshell()
在原始上下文中运行 (这样我就可以从IPython shell访问 a
和 b
)。
Perhaps I could do this with a decorator, but all my attemps so far have failed, since I need ipshell()
to run within the original context (so that I have access to a
and b
from the IPython shell).
我该怎么办?完成这个?
How can I accomplish this?
推荐答案
你可以从另一个中调用 ipshell()
用户定义的功能,例如 ipsh()
You can call ipshell()
from within another user-defined function, e.g. ipsh()
from inspect import currentframe
def ipsh():
frame = currentframe().f_back
msg = 'Stopped at {0.f_code.co_filename} and line {0.f_lineno}'.format(frame)
ipshell(msg,stack_depth=2) # Go back one level!
然后随时使用 ipsh()
放入IPython shell。
Then use ipsh()
whenever you want to drop into the IPython shell.
-
stack_depth = 2
要求ipshell
在检索新IPython shell的命名空间时上升一级(默认值为1
)。 -
currentframe()。f_back()
检索上一帧以便您可以打印调用ipsh()
的位置的行号和文件。
stack_depth=2
asksipshell
to go up one level when retrieving the namespace for the new IPython shell (the default is1
).currentframe().f_back()
retrieves the previous frame so that you can print the line number and file of the location whereipsh()
is called.
这篇关于使用IPython作为有效的调试器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!