Every Python developer eventually hits the same wall: the bug reproduces only on the cluster login node, inside a Docker container, or on a machine where the only interface is an SSH session with eighty columns and no mouse. There is no IDE to attach, no GUI to screenshot, and the first instinct — sprinkling print() calls through the code — turns into an edit-run-edit-run cycle that wastes hours. What you actually want in that moment is a full-screen, in-terminal window where you can see the source, the variables, the call stack, and the breakpoints at the same time, without leaving the terminal.

The standard library offers pdb, and pdb is fine, but it is line-oriented: you scroll a one-line window over a two-thousand-line file and keep the call frame in your head. PuDB takes the opposite bet. It renders a complete debugger screen using the urwid TUI toolkit — source panel with syntax highlighting on the left, variables, stack, and breakpoints stacked in a sidebar on the right, a REPL at the bottom — while reusing the battle-tested bdb tracing machinery underneath. That combination of a modern visual layout with a conservative debugging core is why it has kept a loyal following for over fifteen years.

This post is a source tour of the pudb repository at version 2026.1 (the project uses calendar versioning and targets Python 3.10+). We walk the actual tree: the debugger core, the screen layout, the panels, the theme system, the shell integrations, the settings persistence, and the telnet-based remote debugging mode. Every symbol and line number below was verified against the real source.

Overview architecture of the pudb terminal debugger, showing the debugger core, urwid UI, panels, shells, themes, settings and remote modules

The overview map of pudb: the debugger core and its UI form the spine; panels, shells, themes, and settings hang off the DebuggerUI, while remote debugging subclasses the core and the entry points feed it work.

Reading the overview from left to right:

  • Debugger(bdb.Bdb), L346 — the core tracing class, a bdb.Bdb subclass that refuses to create a second live instance (“a Debugger instance already exists”).
  • set_trace, L419 — the override that deliberately skips bdb.reset() so tracing never dips into bdb’s own source, and that can treat each set_trace() call as a toggleable UI breakpoint.
  • DebuggerUI, L1038 — the entire terminal screen: one class that owns the source column, the sidebar, the command line, and every dialog.
  • FileSourceCodeProvider, L935 — the source feeder that pulls lines through Python’s own linecache.getlines and marks enabled breakpoints.
  • SearchController, L283 — cyclic source search with automatic case-insensitivity when the pattern is all lowercase.
  • VariableWidget, L250 — one node of the variables tree, paired with the ValueWalker family that expands containers, sequences, and mappings.
  • RemoteDebugger, L153 — a Debugger subclass that swaps stdin and stdout for a telnet socket on port 6899.
  • THEMES, L14 — the registry of eleven built-in palettes, from classic and vim to monokai-256 and nord-dark-256.
Detailed architecture of pudb showing Debugger internals, DebuggerUI layout, source and variables panels, shells, theme resolution, settings persistence, remote debugger and entry points

The detail map zooms into modules: how tracing callbacks reach the UI loop, how source lines are highlighted, how palettes resolve, and how every entry point funnels into the same Debugger instance.

The bdb core: one Debugger at a time

pudb/debugger.py is the heart of the project and it is enormous — 3,214 lines — because it hosts both the tracing core and the full UI in one module. The Debugger class extends bdb.Bdb, and its constructor immediately raises ValueError if a debugger instance already exists in the process. That single-instance rule shapes everything else: pudb/__init__.py keeps a class-level _current_debugger list, and helpers like _get_debugger return the existing instance instead of building a new one, so import pudb; pudb.set_trace() from anywhere in your code always lands in the same session.

The set_trace override is where the interesting engineering lives. Standard Bdb.set_trace() calls self.reset(), which causes the debugger to step through bdb’s own source before reaching user code — so pudb skips it and instead walks the frame chain manually, setting f_trace on every caller up the stack. It even branches on the Python version: for 3.13 and newer it saves the frame’s f_trace_lines/f_trace_opcodes flags and forces f_trace_lines = True because the tracing semantics changed. An as_breakpoint flag decides whether the set_trace() call site shows up in the breakpoints panel, where you can disable it with b like any ordinary breakpoint.

Running a script is equally explicit. Debugger._runscript clears the __main__ module dictionary, repopulates it with __name__, __file__, and __builtins__, then compiles and executes the file with exec(compile(open(filename).read(), ...)). Clearing __main__ is not cosmetic — the comment in the source cites issue #331, where pickling broke when the debugged script shared pudb’s __main__ namespace. The same module sets _wait_for_mainpyfile = True so tracing callbacks stay silent until execution reaches the script you actually asked to debug, and installs a SIGINT interrupt handler so Ctrl-C in the debugged program drops you into the UI rather than killing it.

The urwid screen: DebuggerUI’s two-column body

DebuggerUI builds a classic layout: a left-hand column holding the source list plus a command-line pile, and a right-hand sidebar holding the variables, stack, and breakpoints boxes, merged by urwid.Columns at line 1218 with weights taken straight from the config file (sidebar_width, variables_weight, stack_weight, breakpoints_weight). Resizing a panel multiplies its weight by 1.25 and clamps it between one fifth and five, then immediately persists the new geometry through save_config, so your layout survives restarts. The command line at the bottom is a urwid_readline.ReadlineEdit — with emacs-style editing and history — wired to a deque(maxlen=5000) that is loaded from and flushed to internal-cmdline-history.txt under the pudb config directory.

One detail: make_canvas in pudb/ui_tools.py builds the urwid.TextCanvas for source lines and carefully converts per-column attribute runs into per-byte counts using apply_target_encoding. Without that fix, source lines containing wide or non-Latin characters would misalign every highlight after them.

The source panel is built from SourceLine widgets (pudb/source_view.py, line 46), each tracking three visual states — current line, highlighted search hit, breakpoint — and rendering its line number plus text as a single selectable urwid widget. Syntax highlighting is optional and graceful: if Pygments is missing, format_source falls back to plain numbered lines; if it is present, the source goes through PythonLexer and a custom UrwidFormatter whose attribute map (line 267) translates Pygments token types into palette entry names like keyword, string, and comment. There is even a small ArgumentParser state machine — idle, found_function, found_open_paren — that retags names inside function signatures as Token.Argument, so function parameters get their own color.

The variables panel is the most intricate piece. var_view.py defines abstract wrappers PudbCollection, PudbSequence, and PudbMapping that proxy real container objects so the UI can present them uniformly without forcing evaluation of everything, then a ValueWalker hierarchy — BasicValueWalker, WatchValueWalker, and TopAndMainVariableWalker — that recursively builds VariableWidget nodes with expand/collapse state. How a value is rendered is pluggable: get_stringifier (line 527) selects among the default, type, id, and error stringifiers chosen per-frame in the UI, and make_var_view (line 804) assembles the final list. The stack panel rows are StackFrame widgets that show the function name, file, and line; the search box is handled by SearchController, whose perform_search walks the source list cyclically with modulo arithmetic and treats an all-lowercase query as case-insensitive — vim semantics, essentially.

From print() to persistent palettes: build facts

A few concrete numbers and structures worth knowing before you poke at the code. First, the dependency budget is tiny and deliberate: pyproject.toml lists exactly six runtime dependencies — jedi, packaging, pygments (2.19+), urwid (2.5.1+), urwid_readline, and typing_extensions — and builds with hatchling. The duplication of the urwid pin (once >=2.4, then >=2.5.1 with an issue link) documents real compatibility pain in comments. Second, the theme system resolves styles by inheritance: set_style in pudb/theme.py (line 235) recursively climbs INHERITANCE_MAP until it finds a defined parent, so a theme file only overrides the handful of styles it cares about, and get_palette (line 265) will even exec() an arbitrary user theme file if the theme string is a path — falling back with an error message when the file is missing. Third, persistence is version-aware: breakpoint files are named breakpoints-3.12 style, tagged with sys.version_info[:2], so debuggers for different Python minors never clobber each other’s breakpoints. Fourth, the project’s own test suite skips pudb/b.py and pudb/ipython.py via pytest addopts, because those modules intentionally fire side effects on import — and the pytest runner is the tool that exercises the rest of the suite. The codebase is also linted by ruff with a long extend-select list in pyproject.toml, and inline ruff:ignore pragmas mark every deliberate exception. And fifth, pudb/b.py is a two-keystroke debugging trick: it monkeypatches __import__ so that the first execution of import pudb.b anywhere in your program breaks into the debugger on the spot — no code edit required.

Shells, settings, and remote telnet debugging

When you hit ! in the UI, pudb drops into a REPL bound to the stopped frame’s namespace, and the implementation in pudb/shell.py is a study in pragmatic integration. The default classic shell is a code.InteractiveConsole whose namespace is a SetPropagatingDict — a dict subclass that forwards assignments to the real locals and globals so changes you make in the REPL stick to the debugged program. Around it sit adapters for bpython, IPython (with separate v10/v11 paths and a namespace updater), ptpython, and ptipython, plus an run_ipython_kernel option for Jupyter users. The classic shell even persists its own readline history to shell-history beside the config file, and tab completion comes from jedi’s cousin rlcompleter.

Settings live under XDG_CONFIG_HOME/pudb (created with mode 0o700 by get_save_config_path at line 88 of pudb/settings.py). load_config reads pudb.cfg with a ConfigParser, defaults everything from shell and theme to panel weights and the current stringifier, and caches the result in a module-level list so it is read exactly once per process. Breakpoint persistence is separate: parse_breakpoints (line 591) and its save counterpart round-trip breakpoint descriptions through the version-tagged files mentioned above.

Remote debugging is the feature that saves you on clusters and in containers. pudb/remote.py defines RemoteDebugger(Debugger) which redirects stdin/stdout over a TCP socket — default host 127.0.0.1, default port 6899, both overridable via PUDB_RDB_HOST and PUDB_RDB_PORT — and includes a reverse mode (PUDB_RDB_REVERSE) where the debuggee connects back to your nc -l listener. Because telnetlib was removed from CPython in 3.13, the module carries its own TelnetCharacters class with the RFC 854 negotiation bytes (IAC, DO, WILL) copied from the old stdlib code. There is also debug_remote_on_single_rank for MPI jobs, so you can debug rank-by-rank on a cluster. python -m pudb script.py (the CLI in pudb/run.py, exposing pudb.run:main as the console entry point) and the pu.db/pu.go properties injected into builtins round out the entry points.

Honest Limits

The tour would not be honest without limits. The UI depends on urwid’s rendering quirks, so exotic terminals, Windows consoles, and some SSH clients can still glitch — the project classifies itself as beta and POSIX-oriented in its classifiers. Output stealing (--steal-output) exists as a flag but the implementation raises NotImplementedError; the docs are candid that it is not finished. The signal handler that turns Ctrl-C into a debugger entry only installs from the main thread and refuses to overwrite a non-default handler, so programs that already manage SIGINT are on their own. The remote mode speaks plain telnet with no authentication or encryption, which is acceptable on a loopback or a private cluster network and unacceptable on anything shared. The single-instance rule means you cannot nest two interactive pudb sessions in one process, and post-mortem mode through pudb.pm() assumes the exception is the current one. None of these are surprising for a terminal tool, but they matter if you build automation around it.

Watch PyShine on YouTube

Contents