Pdb tracking with Emacs python-mode

October 14, 2022 | View Comments

I've written about my experience with previous Python modes and their horrible feature bloat before.

For a long time, I'd decided to vendor a version of the original Python mode from 2009, because newer alternatives were too painful to use. Unfortunately that same version from 2009 became more and more problematic as Python added new syntax which it couldn't parse correctly.

A few years later, Emacs 24.2 started to finally ship a useful default python-mode, authored by Fabian Ezequiel Gallina.

Now one of my favourite feature from the original Python mode was its Pdb tracking. That is, when you run the Python debugger in shell-mode, say when you hit a breakpoint(), it would helpfully open up the file you're debugging at the relevant line, allowing you to look at the source code in one window while running Pdb commands in your shell window. Despite tons of features, later Python modes failed to include this favourite feature of mine.

Pdb tracking in python-mode

The new default Python mode thankfully brings Pdb tracking back, but it takes a bit of configuration to active it:

(defun my-shell-mode-hook ()
  (add-hook
   'comint-output-filter-functions
   'python-pdbtrack-comint-output-filter-function t))
(add-hook 'shell-mode-hook 'my-shell-mode-hook)