Python Reference ยท Built-in Functions
Python breakpoint() function
Reference for Python breakpoint(): drop into the debugger with one call, the PYTHONBREAKPOINT variable, sys.breakpointhook and how to disable it in production.
The built-in breakpoint() (added in Python 3.7) pauses your program and starts a debugger at that line. By default that debugger is pdb, so you get an interactive prompt where you can print variables, step through code and continue.
It replaces the older import pdb; pdb.set_trace() idiom with a shorter call, and, more importantly, it can be redirected or switched off without editing the code, through the PYTHONBREAKPOINT environment variable.
def total(prices): breakpoint() # the program stops here under pdb return sum(prices)
The in-browser runner has no interactive terminal, so the examples below show what breakpoint() does through its hook rather than opening a real debugger.
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
For real debugging, run the script in a terminal (python script.py), then use these pdb commands at the (Pdb) prompt. This block is read-only.
n run the next line c continue to the next breakpoint s step into a call p x print the value of x l list source around the line q quit the program
Syntax
breakpoint(*args, **kws)
| Name | Description |
|---|---|
*args, **kws |
Passed unchanged to sys.breakpointhook(). The default hook passes them on to the debugger function, so you normally pass none. |
Return value: whatever sys.breakpointhook() returns, usually None.
Raises: RuntimeError if sys.breakpointhook has been deleted.
Usage notes
- It calls
sys.breakpointhook(). The default hook looks atPYTHONBREAKPOINT: unset or empty meanspdb.set_trace,0means do nothing, and a dotted name such asipdb.set_traceorpudb.set_traceimports and calls that function instead. - Disable in production with
PYTHONBREAKPOINT=0, and still search the code for stray calls before releasing; a forgotten breakpoint stalls a server waiting for input. - No source edit needed.
python -m pdb script.pystarts a script under the debugger from its first line. - Post-mortem.
pdb.pm()orpython -m pdbexamines an exception after it happened;breakpoint()stops before. - Editors and IDEs install their own hook so that
breakpoint()opens their debug console. - The default hook is looked up each call, so changing
sys.breakpointhookor the environment variable at runtime affects later calls. - Auditing. Each call raises the
builtins.breakpointaudit event.