使用 IPython 进行分步调试 [英] Step-by-step debugging with IPython

查看:24
本文介绍了使用 IPython 进行分步调试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,有两种方法可以在 Python 中调试代码:

From what I have read, there are two ways to debug code in Python:

  • 使用传统的调试器,例如 pdbipdb.这支持诸如 c 用于 continuen 用于 step-overs 等命令> 用于 step-into 等),但您无法直接访问对对象检查非常有用的 IPython shell.

  • With a traditional debugger such as pdb or ipdb. This supports commands such as c for continue, n for step-over, s for step-into etc.), but you don't have direct access to an IPython shell which can be extremely useful for object inspection.

使用 IPython by 在您的代码中嵌入 IPython shell.您可以执行 from IPython import embed,然后在您的代码中使用 embed().当您的程序/脚本遇到 embed() 语句时,您将被放入 IPython shell.这允许使用所有 IPython 好东西来全面检查对象和测试 Python 代码.但是,在使用 embed() 时,您无法再使用方便的键盘快捷键逐步完成代码.

Using IPython by embedding an IPython shell in your code. You can do from IPython import embed, and then use embed() in your code. When your program/script hits an embed() statement, you are dropped into an IPython shell. This allows the full inspection of objects and testing of Python code using all the IPython goodies. However, when using embed() you can't step-by-step through the code anymore with handy keyboard shortcuts.

有什么方法可以结合两全其美吗?即

Is there any way to combine the best of both worlds? I.e.

  1. 能够使用方便的 pdb/ipdb 键盘快捷键逐步完成您的代码.
  2. 在任何此类步骤(例如在给定语句上),都可以访问成熟的 IPython shell.
  1. Be able to step-by-step through your code with handy pdb/ipdb keyboard shortcuts.
  2. At any such step (e.g. on a given statement), have access to a full-fledged IPython shell.

IPython 调试 MATLAB:

此类增强调试"的示例可以在 MATLAB 中找到,其中用户始终拥有对 MATLAB 引擎/shell 的完全访问权限,并且她仍然可以逐步通过她的代码定义条件断点等等.从我和其他用户的讨论来看,这是人们从 MATLAB 迁移到 IPython 时最怀念的调试功能.

IPython debugging as in MATLAB:

An example of this type of "enhanced debugging" can be found in MATLAB, where the user always has full access to the MATLAB engine/shell, and she can still step-by-step through her code, define conditional breakpoints, etc. From what I have discussed with other users, this is the debugging feature that people miss the most when moving from MATLAB to IPython.

我不想让问题太具体,但我主要在 Emacs 中工作,所以我想知道是否有任何方法可以将这个功能引入其中.理想情况下,Emacs(或编辑器)将允许程序员在代码的任何位置设置断点并与解释器或调试器通信以使其停在您选择的位置,并带来完整的 IPython那个位置的翻译.

I don't want to make the question too specific, but I work mostly in Emacs, so I wonder if there is any way to bring this functionality into it. Ideally, Emacs (or the editor) would allow the programmer to set breakpoints anywhere on the code and communicate with the interpreter or debugger to have it stop in the location of your choice, and bring to a full IPython interpreter on that location.

推荐答案

你可以使用 IPython 的 %pdb 魔法.只需在 IPython 中调用 %pdb,当发生错误时,您会自动跳转到 ipdb.虽然您没有立即进行步进,但之后您会在 ipdb 中.

You can use IPython's %pdb magic. Just call %pdb in IPython and when an error occurs, you're automatically dropped to ipdb. While you don't have the stepping immediately, you're in ipdb afterwards.

这使得调试单个函数变得容易,因为您只需使用 %load 加载一个文件,然后运行一个函数.您可以在正确的位置使用 assert 强制错误.

This makes debugging individual functions easy, as you can just load a file with %load and then run a function. You could force an error with an assert at the right position.

%pdb 是一个行魔法.将其称为 %pdb on%pdb 1%pdb off%pdb 0.如果不带参数调用它作为一个切换.

%pdb is a line magic. Call it as %pdb on, %pdb 1, %pdb off or %pdb 0. If called without argument it works as a toggle.

这篇关于使用 IPython 进行分步调试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆