Skip to content

Debugging Python

New Course Coming Soon:

Get Really Good at Git

Debugging is one of the best skills you can learn, as it will help you in many difficult situations.

Every language has its debugger. Python has pdb, available through the standard library.

You debug by adding one breakpoint into your code:

breakpoint()

You can add more breakpoints if needed.

When the Python interpreter hits a breakpoint in your code, it will stop, and it will tell you what is the next instruction it will run.

Then and you can do a few things.

You can type the name of any variable to inspect its value.

You can press n to step to the next line in the current function. If the code calls functions, the debugger does not get into them, and consider them “black boxes”.

You can press s to step to the next line in the current function. If the next line is a function, the debugger goes into that, and you can then run one instruction of that function at a time.

You can press c to continue the execution of the program normally, without the need to do it step-by-step.

You can press q to stop the execution of the program.

Debugging is useful to evaluate the result of an instruction, and it’s especially good to know how to use it when you have complex iterations or algorithms that you want to fix.

→ Get my Python Handbook
→ Get my Python Handbook

Here is how can I help you: