Introduction to PDB

Type:
Talk
Audience level:
Novice
Category:
Core Python (Language, Stdlib)
March 9th 2:40 p.m. – 3:20 p.m.

Description

PDB is an interactive debugging environment for Python programs. It allows you to pause your program, look at the values of variables, and watch program execution step-by-step, so you can understand what your program is actually doing, as opposed to what you think it's doing. This talk will show novice and intermediate Python users how to use PDB to troubleshoot existing code.

Abstract

PDB is an interactive debugging environment for Python programs. It allows you to pause your program, look at the values of variables, and watch program execution step-by-step, so you can understand what your program is actually doing, as opposed to what you think it's doing.

Effectively using PDB is arguably the most important skill a new Python developer can learn. This talk will show novice and intermediate Python users how to use PDB to troubleshoot existing code.

  • When is it reasonable to use PDB?

    • "I don't use a debugger"

    • When is it really not reasonable?

  • Modes of pdb usage

    • set_trace mode, e.g. pdb.set_trace()

    • postmortem mode, e.g. python -m pdb buggy.py or pdb.pm()

    • run mode, .e.g. pdb.run('some.expression()').

  • Getting help

  • Shortcut aliases (c vs. continue)

  • The workhorse commands (list, print, pretty-print, next, continue, step, return, until, where, up, down):

    • list: displaying code in your current execution context

    • p and pp: displaying objects

    • continue, step, return, next, return, until: execution control

    • where: showing the current location in the frame stack

    • up, down: navigating the frame stack

  • Managing breakpoints (break, tbreak, ignore, enable, disable, clear):

    • break, tbreak, ignore, enable, disable, and clear: Managing breakpoints
  • Lesser-used commands (args, !-prefixing, debug)

    • debug: recursive debugging

    • !-prefixing: modifying variables

    • args: printing args to the current function

    • commands: scripting pdb

  • ~/.pdbrc and PDB aliases

  • Debugging in the face of threads (ie. web apps).

  • "Purple bags"

    • Enhanced shells: ipdb, pudb, winpdb

    • In-editor debugger integration (Wing, Eclipse PyDev, PyCharm, etc)