pdb Verified current stable Not installed? Programming

Pdb / Debug Python Script Interactively

Debug Python Script Interactively

Debugs a specified Python script interactively using pdb.

$
Terminal
python -m pdb <path/to/file.py>

When To Use

When needing to analyze and fix bugs in scripts during development.

Pro Tip

Set breakpoints within the code for targeted debugging before execution starts.

Anatomy of Output

Understanding the result

> path/to/file.py(10)<module>() Current Frame

Indicates the current line of execution.

(Pdb) list Debugger Command

Shows the surrounding code.

(Pdb) continue Debugger Command

Resumes execution until the next breakpoint.

Troubleshooting

Common pitfalls

FileNotFoundError: [Errno 2] No such file or directory: 'path/to/file.py'

Solution: Verify the script file path.

ImportError: cannot import name 'nonexistent'

Solution: Check for correct module and function names.

AttributeError: 'NoneType' object has no attribute 'method'

Solution: Investigate potential None returns from previous function calls.

Command Breakdown

What each part is doing

python
Base Command
The executable that performs this operation. Here it runs Pdb before the shell applies any redirect operators.
<path/to/file.py>
Input Files
The file path or paths supplied to this command.
-m
Command Option
Tool-specific option used by this command invocation.

Alternative Approaches

Comparable commands in other tools

Alternative programming tools for the same job.