56

How do I debug python programs in emacs? I'm using python-mode.el

I've found references suggesting:

import pdb; pdb.set_trace();

but I'm not sure how to use it.

Att Righ
  • 1,439
  • 1
  • 16
  • 29

3 Answers3

49

Type M-x cd to change directory to the location of the program you wish to debug. Type M-x pdb. You'll be prompted with Run pdb (like this): pdb. Enter the name of the program (e.g. test.py).

At the (Pdb) prompt, type help to learn about how to use pdb.

Alternatively, you can put

import pdb 
pdb.set_trace()

right inside your program (e.g. test.py). Now type M-x shell to get a shell prompt. When you run your program, you'll be dumped into pdb at the point where pdb.set_trace() is executed.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • Using pdbtrack (through M-x shell) is probably a better solution. It's much easier to control directories and environments that way. – Matt Harrison Feb 24 '10 at 15:00
  • 9
    @matt with M-x pdb you can set break points in source files and a cursor will show the current position of execution as you step through: http://twistedmatrix.com/documents/current/core/howto/debug-with-emacs.html – Justin Smith Feb 25 '10 at 23:59
  • 2
    A graphical tutorial may be found here: http://sunnyeves.blogspot.com/2011/04/debugging-python-on-emacs-ide.html – gt6989b Apr 19 '12 at 22:00
  • 1
    http://stackoverflow.com/questions/9167614/python-mode-in-emacs-no-such-file-or-directory-pdb might be helpful if you don't have a pdb executable, see @sanityinc's comment: create a shell script pdb in any of the dir's in your path with text: #!/bin/bash exec python -m pdb "$@" – 0fnt May 12 '12 at 12:40
  • Why the method one doesn't work for me. emacs complains that it cannot find the provided .py file. – q0987 Mar 01 '13 at 16:29
  • anyone know how to get the 'red-dot' on breakpoints similar to gdb? – bph Mar 21 '16 at 11:00
  • The key binding to set a breakpoint is C-x C-a C-b in recent emacs versions. – amoe Nov 17 '21 at 10:15
22

For me, I needed to replace the default "pdb" with

python -m pdb myscript.py
Ben
  • 9,184
  • 1
  • 43
  • 56
20

The realgud package (available from MELPA) supports PDB (among a gazillion other debuggers), and has a host of neat features that Emac's PDB doesn't have.

The one I like best is the shortkeys mode. Once you start debugging a program, you can press n, s, c etc. right in the source window, instead of having to type these commands in the PDB buffer. It also supports Visual-Studio style keybindings with function keys (f10, f11, f5, etc).

After installing RealGUD, you need to run M-x load-feature realgud to load it, and you can start pdb with M-x realgud:pdb.

Clément
  • 12,299
  • 15
  • 75
  • 115
  • 7
    I should add that I have a small conflict of interest. I found the package so neat that I started hacking on it, and now became a co-maintainer. – Clément Jul 09 '16 at 09:28
  • realgud looks amazing! I wonder how straightforward it would be to integrate entering realgud/emacs with existing debug-entry flows like setting a trace and running a test with something like `pytest`. Any tips here? Maybe this will do what I'm looking for: https://github.com/realgud/realgud/#tracking-an-existing-debugger-process – mindthief Aug 16 '19 at 20:40
  • realgud isn't supported in emacs-25, attempting to install it gives "Package `emacs-25` is unavailable" error message, unfortunately till it gets supported by emacs-25 I will need to find an alternative python debugger for emacs-25, or just keep using pdb without the emacs integration – AmazingMiki Feb 05 '20 at 12:27
  • You have it backwards: it *requires* emacs-25, and you're trying to install it in an older Emacs. (see https://melpa.org/#/realgud for the dependencies) – Clément Feb 05 '20 at 19:40
  • looks cool, but it does not seem to be maintained recently. @Clément do you know what the story is? Also in emacs-27 `M-x load-feature` does not do anything after a melpa install. – markgalassi Jul 15 '22 at 03:01
  • Lack of time is the main story. It works OK for me, but I haven't had much time to dedicate to it. I head that lsp-mode has support for DAP, but I don't know if that supports Python – Clément Jul 16 '22 at 06:02