Sub topic 12
Saving your setup in a .pdbrc file
This chapter assumes you can start a pdb session with breakpoint() and use basic commands such as p, n, and b. If you retype the same aliases or breakpoints at the start of every session, a .pdbrc file can do it for you.
What a .pdbrc file is
A .pdbrc file is a plain text file of pdb commands. When pdb starts, it reads the file and runs each line as if you had typed it at the (Pdb) prompt. Each line is one command.
pdb looks for the file in two places, in this order:
.pdbrcin your home directory. Use it for personal settings..pdbrcin the current working directory. Use it for settings that belong to one project.
Both files are read if both exist. The home file runs first, then the local one.
Lines that start with # are comments and pdb ignores them. Blank lines are ignored too.
A first example
This file defines two aliases and one display setting.
# ~/.pdbrc
alias pl pp locals()
alias pi for k, v in %1.__dict__.items(): print(f"{k} = {v!r}")An alias is a short name for a longer command. The alias command takes a name, then the text it stands for. The placeholder %1 is replaced by the first argument you give. %* stands for all arguments.
After this file loads, typing pl runs pp locals(). Typing pi self prints every attribute stored on self.
You can list your aliases with alias and no arguments. You can remove one with unalias pl.
Project settings
A project .pdbrc can set up the breakpoints you always need. Suppose the project has a function process_item in app.py that often misbehaves.
# ./.pdbrc
break app.py:42
tbreak process_item
alias pr p repr(%1)Every session started from this directory now begins with a breakpoint on line 42 of app.py and a temporary breakpoint on process_item. Breakpoints in .pdbrc only work when the file and line exist. If the line number is wrong, pdb prints an error when it loads the file, and the other lines still run.
Running Python code from .pdbrc
A line that starts with ! runs as a Python statement. This is useful for imports you want available in every session.
import pprint
!import pprint
alias pp2 pprint.pprint(%1, width=60)The second line imports pprint into the debugged frame's namespace. The first line, without the !, is not a valid command and is treated as a Python statement too, because pdb runs any line that is not a pdb command as Python. Use ! anyway, since it also works when a Python name matches a pdb command, such as a variable called c or n.
Common errors
- The file is ignored. Check the file name. It must start with a dot and have no extension. Check that you started Python from the directory that holds it.
- A command stops the rest of the file. Commands that resume execution, such as
continueornext, should not appear in.pdbrc. Put only setup commands there. - Aliases with wrong arguments. An alias that uses
%1fails with an error if you call it with no argument. Test each alias once by hand. - Surprising project files. A
.pdbrcin the working directory runs automatically. Only run Python from directories you trust, because the file can execute arbitrary Python code.
Exercises
Exercise
You have both ~/.pdbrc and ./.pdbrc. Both define the alias pl, with different text. Which definition is active in the session, and why?
Show answer
The one in ./.pdbrc. pdb reads the home file first and the current directory file second, so the later alias pl ... line replaces the earlier one.
Exercise
Write a .pdbrc line that creates an alias pt which prints the type of its first argument, so pt x runs p type(x).
Show answer
alias pt p type(%1)