Stop debugging Python with print | Andros Fenollosa
Even though breakpoint() has been available since Python 3.7, many developers still reach for print to debug. Maybe out of habit or fear. Here is a quick example to get you started.
Suppose you have a shopping cart and want to calculate the total, applying a discount. You might write something like this:
def calculate_total(items):<br>total = 0<br>for item in items:<br>breakpoint() # Execution will stop here<br>total += item["price"] * item["quantity"]<br>return total<br>When execution reaches that line, the program stops and gives you a (Pdb) prompt. From there you can:
Inspect variables: p item, p total
Step line by line: n (next)
Step into functions: s (step)
Continue to the next breakpoint: c (continue)
Exit the debugger: q (quit)
You can also run any valid Python expression, modify variables, call functions, and so on.
Three ways to run the script
Normal execution
To debug, run the script as usual. The program will stop at the first breakpoint() and open the interactive (Pdb) console:
uv run demo.py<br>Ignore all breakpoints
The script runs straight through without pausing. Useful when you have finished debugging but have not removed the breakpoint() calls yet, or when running in a production environment.
PYTHONBREAKPOINT=0 uv run demo.py<br>Post-mortem mode
Useful for scripts that have no breakpoint() calls. The script runs normally and, if something crashes, drops you into the pdb console at the exact point of failure so you can inspect the state.
uv run python -m pdb -c continue demo.py<br>Commands
Command<br>Action
n (next)<br>execute the current line without stepping into functions
s (step)<br>same, but steps into the called function
c (continue)<br>resume until the next breakpoint or the end
l (list)<br>show the code around the current line
p expr<br>print a variable or expression: p total
pp expr<br>same but pretty-printed, useful for dicts and lists
w (where)<br>show the call stack
a (args)<br>show the arguments of the current function
r (return)<br>execute until the current function returns
q (quit)<br>abort execution
h (help)<br>general help; h for details on a specific one
Final notes
The benefits of using breakpoint() and pdb are clear:
It keeps the code clean, no print statements to remove afterwards.
You can inspect the program state at any point.
You can modify variables and test changes on the fly.
You can pause execution at any moment, not just when something crashes.
And it is a standard Python tool, no installation required.
Give it a try and see how it improves your workflow.
Happy debugging!
Three ways to run the script
Normal execution
Ignore all breakpoints
Post-mortem mode
Commands
Final notes
This work is under a Attribution-NonCommercial-NoDerivatives 4.0 International license.
Will you buy me a coffee?
Support me on Ko-fi
Comments
page#run"<br>data-liveview-function="show_comment_email"<br>data-id="e380cf75"<br>data-type="article"<br>>Leave a comment
There are no comments yet.
You may also like
page#run"<br>data-liveview-function="navigate_article"<br>data-uuid="5afd804f">
programaci贸n funcional
python
funcional
Constants and pure functions in Python: how to do it right
page#run"<br>data-liveview-function="navigate_article"<br>data-uuid="3225552c">
python
Python Comments and Docstrings Best Practices
page#run"<br>data-liveview-function="navigate_article"<br>data-uuid="158e0835">
python
Encrypting and decrypting data in Python
Visitors in real time
You are alone: 馃惐