Python Engineer

Free Python and Machine Learning Tutorials

Become A Patron and get exclusive content! Get access to ML From Scratch notebooks, join a private Slack channel, get priority response, and more! I really appreciate the support!

Python Quick Tip: Debugger and breakpoint()

17 Sep 2020

Learn how to use the Python Debugger using the breakpoint() function in this Tutorial.

Whenever your code does not work as expected and you want to find out why, you can utilize the Python debugger to detect the bug. It's very simple to get started. You can insert a breakpoint with the breakpoint() function at any position in your code . This is new in Python 3.7, and is equivalent to the older import pdb; pdb.set_trace() command.

# a = int(0.1) b = 3.0 c = a + b breakpoint() # a lot of more code here...

Now when we run this file with python, it will stop at the breakpoint and start the debugger session:

python --Return-- ><module>()->None -> breakpoint() (Pdb)

Now you can apply different commands, e.g., print variables/expressions or execute the script line by line. The most useful commans are listed below. For a full list have a look at the official documentation here.

With these commands, we can step through the code, print expressions, and track down our bug 😊.

Further readings: