如何逐步处理Python代码以帮助调试问题? [英] How to step through Python code to help debug issues?

查看:113
本文介绍了如何逐步处理Python代码以帮助调试问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java / C#中,您可以轻松地逐步执行代码以跟踪可能出了什么问题,而IDE使此过程非常人性化。

In Java/C# you can easily step through code to trace what might be going wrong, and IDE's make this process very user friendly.

您可以通过python进行跟踪吗?

Can you trace through python code in a similar fashion?

推荐答案

是的!为此,有一个名为 pdb 的Python调试器!

Yes! There's a Python debugger called pdb just for doing that!

您可以通过<$ c $启动Python程序c> pdb 使用 pdb myscript.py python -m pdb myscript.py

随后您可以发出一些命令,这些命令记录在 pdb 页。

There are a few commands you can then issue, which are documented on the pdb page.

需要记住的一些有用的方法是:

Some useful ones to remember are:


  • b :设置断点

  • c :继续调试,直到遇到断点为止

  • s :步骤通过代码

  • n :转到下一行代码

  • l :列出当前文件的源代码(默认值:11行,包括正在执行的行)

  • u :向上浏览堆栈框架

  • d :向下浏览堆栈框架

  • p :打印a的值当前上下文中的n表达式

  • b: set a breakpoint
  • c: continue debugging until you hit a breakpoint
  • s: step through the code
  • n: to go to next line of code
  • l: list source code for the current file (default: 11 lines including the line being executed)
  • u: navigate up a stack frame
  • d: navigate down a stack frame
  • p: to print the value of an expression in the current context

如果您不想使用命令行调试器,则某些IDE如 Pydev Wing IDE PyCharm 具有GUI调试器。 Wing和PyCharm是商业产品,但是Wing具有免费的个人版本,PyCharm具有免费的社区版本。

If you don't want to use a command line debugger, some IDEs like Pydev, Wing IDE or PyCharm have a GUI debugger. Wing and PyCharm are commercial products, but Wing has a free "Personal" edition, and PyCharm has a free community edition.

这篇关于如何逐步处理Python代码以帮助调试问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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