ipython -pylab 调试:我可以在特定行停止执行并放入 shell 中吗?

发布于 2024-11-03 07:34:12 字数 799 浏览 1 评论 0原文

在编写Python代码(主要是numpy + matplotlib)时,我通常只是在vim中输入代码并运行程序来测试它:

python2 foo.py有时

,当这还不够并且我需要更彻底地检查问题时,我只是在 ipython 中启动程序: ipython -pylab foo.py,然后检查变量,测试一些命令等等。我喜欢 ipython,因为 Tab 补全功能和 bash 命令的可用性。

这对我来说已经足够好了,但现在我的程序变得更大并且包含许多子例程(在多个文件中)。 ipython 方法不再有效,因为它总是运行完整代码,直到 foo.py 末尾(当它落入pylab 外壳)。我想做的是,在子例程中的给定行(可能在另一个文件中)停止执行并检查那里的变量。 即设置一个 pylab shell 启动的断点。

有没有一种简单的方法来适应我的 ipython 工作方式?例如,在 bar.py 中的一行处停止

ipython -pylab --stop-at bar.py:423 foo.py

,或者在 bar.py 中的子例程名称处停止

ipython -pylab --stop-at bar.py:subroutine-name foo.py

When writing python code (mostly numpy + matplotlib), I usually just type the code in vim and run the program to test it:

python2 foo.py

Occasionally, when this is not sufficient and I need to inspect the problem more thoroughly, I just launch the program in ipython:
ipython -pylab foo.py, and then inspect the variables, test some commands and so on. I like ipython, because of the tab completion and the availability of bash commands.

This worked well enough for me, but now my programs grew bigger and include many subroutines (in multiple files). The ipython approach doesn't work any more, because it always runs the complete code till the end of foo.py (when it drops into the pylab shell). What I'd like to do instead is, stop execution at a given line in a subroutine (could be in another file) and inspect variables there. I.e. set a break point at which the pylab shell kicks in.

Is there an easy way to adapt my ipython way of working? E.g. stop at a line in bar.py

ipython -pylab --stop-at bar.py:423 foo.py

or, stop at a subroutine name in bar.py

ipython -pylab --stop-at bar.py:subroutine-name foo.py

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟─花易冷 2024-11-10 07:34:12

您可以将 pdb 模块导入到代码中,然后在您希望代码停止的位置添加 pdb.set_trace() 调用。 Ipython 将进入交互式调试器,您可以根据需要自由地单步调试代码。

You can import the pdb module into the code and then add a pdb.set_trace() call where you would like to have the code stop. Ipython will drop into the interactive debugger, and you are free to step though your code as you wish.

绅士风度i 2024-11-10 07:34:12

您可以通过在所需的位置插入以下代码来进入 IPython 调试会话:

import sys, IPython
IPython.Shell.IPShell(argv=[])
IPython.Debugger.Pdb(IPython.ipapi.get().options.colors).set_trace(sys._getframe())

不完全是您想要的内容,但它对我来说效果很好。它还使得设置复杂的条件断点变得非常容易。还有其他几种方法可以从网络上的源文件中启动 IPython 调试会话,但是根据我的经验,这在加载正确的颜色、选项卡完成正常工作等方面是最可靠的

。调试会话已启动,您可以使用 break 命令设置更多断点:

ipdb> break test.py:11
Breakpoint 1 at /tmp/test.py:11
ipdb> b my_function
Breakpoint 2 at /tmp/test.py:5

为了方便插入,您可以在编辑器中设置宏/组合键。我也是 Vim 用户,我的 vimrc 中有以下键盘映射:

nmap <C-P><C-D> oimport sys, IPython<CR>IPython.Shell.IPShell(argv=[])<CR>IPython.Debugger.Pdb(IPython.ipapi.get().options.colors).set_trace(sys._getframe())<ESC>:w<CR>

在正常模式下,按 Ctrl-P,然后按 Ctrl-D 会在当前代码之后插入调试代码使用正确的缩进,然后保存文件。

You can drop into a IPython debug session by inserting the following code at the desired point:

import sys, IPython
IPython.Shell.IPShell(argv=[])
IPython.Debugger.Pdb(IPython.ipapi.get().options.colors).set_trace(sys._getframe())

Not exactly what you seem to be looking for, but it works quite nicely for me. It also makes it really easy to have complex conditional breakpoints. There are several other methods of starting an IPython debug session from within your source file floating around on the web, but this - in my experience anyway - is the most reliable in terms of loading the correct colours, tab completion working properly etc.

Once the debug session has started you can set further breakpoints using the break command:

ipdb> break test.py:11
Breakpoint 1 at /tmp/test.py:11
ipdb> b my_function
Breakpoint 2 at /tmp/test.py:5

To make it easy to insert, you can set a macro/key combination in your editor. I'm also a Vim user and I have the following keymap in my vimrc:

nmap <C-P><C-D> oimport sys, IPython<CR>IPython.Shell.IPShell(argv=[])<CR>IPython.Debugger.Pdb(IPython.ipapi.get().options.colors).set_trace(sys._getframe())<ESC>:w<CR>

From normal mode, pressing Ctrl-P then Ctrl-D inserts the debug code after the current line with the correct indentation and then saves the file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文