分享

调试器

 dbn9981 2022-10-08 发布于北京

通过与IPython控制台中的增强调试器集成,支持Spyder中的调试。这允许直接从Spyder GUI以及所有熟悉的IPython控制台命令查看和控制断点和执行流。ipdb

一个间谍 IPython 控制台窗口,显示正在运行的 ipdb 调试器

使用 ipdb 进行调试

您可以从“调试”菜单、“调试”工具栏、可配置的键盘快捷方式以及标准控制台命令完全控制调试器的执行。ipdb

一个间谍IPython控制台窗口,显示工具栏中的调试

此外,编辑器还使用箭头显示调试器当前停止的代码行。

显示调试面板的 Spyder 编辑器

Spyder的调试器提供语法突出显示,代码完成和命令历史记录,其工作方式与普通交互式解释器完全相同。

使用向上和向下箭头调用以前的命令,然后按键触发自动完成建议。Tab

一个间谍IPython控制台窗口,在调试时显示自动完成

Furthermore, IPython’s magic functions are available in debugging mode. You can, for example, run to list the contents of your current working directory or to check how fast a given snippet of code is.%ls%timeit

Ipython console in debug mode showing timeit magic

Finally, you can enter and execute multiline statements in Spyder’s debugger just like with the regular IPython prompt, to easily run complex code.

Debugger showing multiline debugger

Breakpoints

Spyder’s debugger is integrated with the Breakpoints pane, which lists the file, line, and condition (if any) of every breakpoint defined. To open it, select Debug ‣ List breakpoints, or press ( on macOS).Ctrl-Shift-BCmd-Shift-B

Spyder's Breakpoints panel, with a number of examples showing file, line number and an optional condition

There are several different ways to set and clear breakpoints:

  • With the Set/clear breakpoint option in the Debug menu.

  • Through pressing the configurable keyboard shortcut ( for normal, or for conditional breakpoints by default).F12Shift-F12

  • By clicking to the left of the line number in an open file in the Editor (adding for a conditional breakpoint).Shift

  • With the builtin function in your code.breakpoint()

  • Interactively, using the command in a debugging session.b

Spyder showing setting conditional breakpoint

You can access and edit local and global variables at each breakpoint through the Variable Explorer.

Spyder's console and variable explorer showing local and global variables when debugging

Advanced features

You can avoid stepping through other Python packages while debugging by enabling the new Ignore Python libraries while debugging option in Spyder’s preferences, under IPython Console ‣ Debugger ‣ Debug. This will skip all the built-in and third-party Python modules you have installed.

Spyder's preferences showing Ignore Python libraries while debugging options

If your code has variables with the same names as Pdb commands (e.g. or ), you can still refer to them as normal while debugging. To call the respective Pdb command, just add an exclamation point before it (e.g. or ).bstep!b!step

Spyder's IPython console showing Pdb commands

You can have Spyder automatically execute a custom snippet of code every time the debugger stops. For example, you can use this to set specific variables, or import commonly-used modules so they are always available while debugging. To set this up, go to Preferences ‣ IPython Console ‣ Debugger ‣ Run code while debugging, and enter the code that you want to be executed with each step.

Spyder's preferenes with debugging snippet

Matplotlib support

Generating Matplotlib figures is fully supported while the debugger is active, including all the different graphics backends. Use the magic to change to an interactive backend (e.g. ) to pan, zoom and adjust your plots in a separate window, or switch back to the default () to see them displayed right in the Plots pane.%matplotlib%matplotlib qt5inline%matplotlib inline

Debugger showing matplotlib interactive backend

To avoid showing plots while debugging, deactivate the Process execute events while debugging option in Preferences ‣ IPython console ‣ Debugger.

Related panes

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多