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 a similar fashion?
Yes! There's a Python debugger called pdb
just for doing that!
You can launch a Python program through pdb
via 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 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 contextIf 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.