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

查看:25
本文介绍了如何单步调试 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!

您可以使用 pdb myscript.pypython -m pdb myscript.py 通过 pdb 启动 Python 程序.

You can launch a Python program through pdb by using pdb myscript.py or 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:打印当前上下文中表达式的值
  • 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,例如 PydevWing IDEPyCharm 有一个 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天全站免登陆