Python Debugging

Python Debugging with pdb

amirreza-km Updated

File contents

Python Debugging with pdb

Description

A standard procedure for debugging Python code using the built-in pdb module.

When to use

When you need to step through Python code execution to inspect variables and control flow.

Requirements

  • Python installed.

Procedure

  1. Import pdb.
  2. Insert pdb.set_trace() where you want execution to pause.
  3. Run the script.
  4. Use commands like n (next), s (step), c (continue), and p <var> (print variable).

Examples

import pdb

def buggy_function():
    x = 10
    pdb.set_trace()
    y = x / 0
    return y

Common mistakes / Pitfalls

Do not leave pdb.set_trace() calls in production code. Use conditional breakpoints or logging for production.

amirreza-km/skill-memory-mcp/tree/main/skill-memory-mcp/skills/examples/python-debugging commit 17476569cc

Frequently asked questions

npx skillmds@latest add amirreza-km/python-debugging