Skip to content

Instantly share code, notes, and snippets.

@supermacro
Created March 21, 2025 14:49
Show Gist options
  • Select an option

  • Save supermacro/70ccc20d410fc95e03bc54fa8e1035a5 to your computer and use it in GitHub Desktop.

Select an option

Save supermacro/70ccc20d410fc95e03bc54fa8e1035a5 to your computer and use it in GitHub Desktop.

Python Debugger Cheatsheet

Here's a comprehensive cheatsheet for Python's debugging capabilities:

PDB (Python Debugger)

Starting the Debugger

# Insert at any point in your code
import pdb; pdb.set_trace()   # Python 2 and 3
breakpoint()                  # Python 3.7+

Basic Navigation Commands

  • c / cont / continue: Continue execution until next breakpoint
  • n / next: Execute current line and move to next line (steps over function calls)
  • s / step: Step into function call
  • r / return: Continue execution until current function returns
  • q / quit: Exit the debugger

Examining State

  • l / list: Show current position in code
  • ll / longlist: Show more context around current position
  • p expression: Print value of expression
  • pp expression: Pretty-print value of expression
  • whatis expression: Print type of expression
  • dir(object): Show attributes of object
  • locals(): Show all local variables
  • globals(): Show all global variables

Breakpoint Management

  • b / break: Set breakpoint at current line
  • b linenum: Set breakpoint at line number
  • b file.py:linenum: Set breakpoint in file at line number
  • b function: Set breakpoint at first line of function
  • disable bpnum: Disable breakpoint number
  • enable bpnum: Enable breakpoint number
  • clear bpnum: Delete breakpoint number
  • clear: Delete all breakpoints

Advanced Commands

  • j linenum: Jump to line number (skips execution)
  • unt / until: Continue until line with greater line number is reached
  • unt linenum: Continue execution until line number
  • a / args: Print arguments of current function
  • u / up: Move up one frame in the call stack
  • d / down: Move down one frame in the call stack
  • w / where: Print current call stack
  • h / help: Show list of commands
  • h command: Show help for specific command
  • !statement: Execute Python statement in current context

IPython Debugger (ipdb)

# Install with: pip install ipdb
import ipdb; ipdb.set_trace()

IPython debugger has all PDB commands plus tab completion, syntax highlighting, and better printing of variables.

Embedding into Frameworks

Jupyter Notebook

from IPython.core.debugger import set_trace
set_trace()  # Or use %debug magic after an exception

Django

# In settings.py, set DEBUG=True
from django.conf import settings
if settings.DEBUG:
    import pdb; pdb.set_trace()

Flask

# Debug on exception
app.debug = True

# Or manual breakpoint
import pdb; pdb.set_trace()

Web-PDB (Remote Debugging)

# Install with: pip install web-pdb
import web_pdb; web_pdb.set_trace()  # Then open browser at http://localhost:5555

Using the VS Code Debugger

Add this to .vscode/launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        }
    ]
}

Then use F5 to start debugging with breakpoints set in the editor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment