58

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.

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Using pdbtrack (through M-x shell) is probably a better solution. It's much easier to control directories and environments that way.
@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: twistedmatrix.com/documents/current/core/howto/…
A graphical tutorial may be found here: sunnyeves.blogspot.com/2011/04/…
stackoverflow.com/questions/9167614/… 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 "$@"
Why the method one doesn't work for me. emacs complains that it cannot find the provided .py file.
|
22

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

python -m pdb myscript.py

Comments

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.

6 Comments

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.
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: github.com/realgud/realgud/…
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
You have it backwards: it requires emacs-25, and you're trying to install it in an older Emacs. (see melpa.org/#/realgud for the dependencies)
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.
|