Step-by-step debugging with IPython
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
IPython debugging gives a faster feedback loop than print-debugging because you can inspect and mutate runtime state interactively. The key is to follow a repeatable process: reproduce, breakpoint, inspect, verify assumptions, then patch. This approach turns debugging into a controlled investigation instead of guesswork.
Start from a Reproducible Failure
Debugger sessions are only useful if failure can be reproduced consistently. Build a minimal failing input before stepping through code.
For script-level debugging:
Useful commands:
- '
lshow source lines.' - '
nexecute next line.' - '
sstep into function.' - '
ccontinue to next breakpoint.' - '
p exprprint expression.' - '
qquit.'
Insert Breakpoints with IPython
Use set_trace near the suspicious state transition.
At the breakpoint, inspect locals and evaluate expressions in context.
Post-Mortem Debugging
When program already crashed, inspect failure context without restarting from the beginning.
%debug opens stack at exception frame and is very effective for long-running tasks.
Structured Investigation Loop
Use a consistent loop:
- Reproduce with minimal input.
- Breakpoint before failure.
- Inspect local state and call stack.
- Validate invariants.
- Apply minimal fix.
- Add regression test.
Example invariant checks:
Assertions make implicit assumptions explicit.
Debugging in Notebooks
Notebook state can hide bugs due to out-of-order execution. For reliable debugging:
- Restart kernel.
- Run cells in order to failure.
- Add breakpoints and inspect.
- Re-run from clean state after patch.
Skipping this discipline often causes stale-variable confusion.
Async Debugging Pattern
Async code can be debugged the same way, but execution includes await boundaries.
Inspect task flow and awaited calls when control path appears non-linear.
Combine Debugging with Logs
Interactive debugging helps local diagnosis, while logs provide production context. Use both:
- Logs for historical request trace and identifiers.
- Breakpoints for precise runtime inspection.
When fixing incidents, correlate debugger findings with real logs to avoid local-only assumptions.
Turn Findings into Guardrails
After fixing bug:
- Remove temporary breakpoints.
- Add regression test.
- Document root cause briefly in issue tracker.
- Keep minimal reproducible example.
This converts debugging effort into long-term reliability.
Useful Interactive Commands During Breakpoints
Inside an IPython breakpoint, a few commands consistently save time:
- '
whereto print stack frames.' - '
upanddownto move through call frames.' - '
pp varfor readable pretty-print output.' - '
!commandto run a quick shell command without leaving the session.'
During that pause, inspect values, evaluate max(values), and verify assumptions before changing code.
Common Pitfalls
- Debugging without deterministic reproducer.
- Changing many things before confirming root cause.
- Forgetting to remove breakpoints before commit.
- Trusting notebook state not rebuilt from fresh kernel.
- Fixing symptom only without adding regression coverage.
Summary
- IPython debugging is most effective with reproducible failures.
- Use breakpoints for live state and
%debugfor post-mortem crashes. - Follow a structured investigation loop to reduce trial-and-error.
- Pair interactive debugging with logs for complete diagnosis.
- Always convert confirmed fixes into automated regression tests.
Related reading
- Stop pip from failing on single package when installing with requirements.txt
- Stop Tensorflow from printing to the console
- Stopping/Purging Periodic Tasks in Django-Celery
- Store output of subprocess.Popen call in a string
- Step Into Property/Function F11 doesn't work as expected
- Stop developer tools access needs to take control of another process for debugging to continue alert
- Storing Python dictionaries
- ''str'' object does not support item assignment
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.