如何逐步处理Python代码以帮助调试问题? [英] How to step through Python code to help debug issues?
问题描述
在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 breakpointc
: continue debugging until you hit a breakpoints
: step through the coden
: to go to next line of codel
: list source code for the current file (default: 11 lines including the line being executed)u
: navigate up a stack framed
: navigate down a stack framep
: 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屋!