How to step through Python code to help debug issues?

前端 未结 14 1071
梦如初夏
梦如初夏 2020-11-22 11:17

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.

Can you trace through python code in

14条回答
  •  执笔经年
    2020-11-22 11:26

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

    You can launch a Python program through pdb by using pdb myscript.py or python -m pdb myscript.py.

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

    Some useful ones to remember are:

    • 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

    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.

提交回复
热议问题