You can debug Python in Emacs using PDB. If you have a standalone pdb installed you can use M-x pdb to start the process. Otherwise, you will have to import pdb in your code and set breakpoints using the code pdb.set_trace(). The second way is described here.

Setup

  1. Write your program.
  2. Add import pdb

  3. Add pdb.set_trace() wherever you want to set a breakpoint

  4. Run your program: C-c C-c. This should split the screen between your code and the pdb interactive prompt*.

  5. Once PDB hits a breakpoint, you should see a black triangle in the left fringe. You can step forward in your code and see what values your variables take on. See below for a short list of commands.

OR

  1. Write your program in file 'program.py' with a main function 'main'.
  2. Add pdb.set_trace() wherever you want to set a breakpoint

  3. At the interactive prompt (press C-c C-z to get here), import pdb.

  4. Then import program.

  5. Then pdb.run('program.main()')

  6. PDB takes over the interactive prompt. Press c (continue) to run your program to the first breakpoint. Or single-step with s.

  7. Once PDB hits a breakpoint, you should see a black triangle in the left fringe. You can step forward in your code and see what values your variables take on. See below for a short list of commands.

When your program is finished, PDB will return you to the normal interactive mode.

*But on my setup it opens the currently executing code in a temp file after pdbtrack says that a traceback cue was not found. It still seems to work.

Commands

Of course, before you use a debugger, find out if there's a simpler way to find out what's going wrong. One way to do this is with print statements, either to stdout or to a log file. Automated use of print at function call boundaries is extremely useful. It ships as a library in Scheme, but you'll have to write your own in Python: Trace Function Calls in Python.

Other Commands

Notes

Here is the PDB module documentation.

If your program is crashing and the exception doesn't provide enough detail, PDB also provides post-mortem debugging.

   1 >>> import pdb
   2 >>> import crashy
   3 >>> crashy.main()
   4 KeyError: 'foo'
   5 Traceback (most recent call last):
   6   File "crashy.py", line 10, in main
   7     print d['foo']
   8 KeyError: 'foo'
   9 # Oh no! A KeyError! Why???
  10 >>> pdb.pm()
  11 > /Users/ncsander/src/crashy.py(10)main()
  12 ->     print d['foo']
  13 (Pdb) p d
  14 {}
  15 #Oh. d is empty

The instructions work on Aquamacs and the default emacs installed on OS 10.4. You get nice fringe displays on Aquamacs, while the terminal-based emacs just overwrites the first two characters of the line with =>.

PDB behaves strangely with generators. It appears to run the generator to completion, then return the values. While stepping through the running part, you can not inspect the generator's bound variables. You can only do so during the return phase. (This is a preliminary observation, and probably wrong.) A common use of generators is something like sum(x*y for x,y in zip(list1,list2)).

See also: Debug C in Emacs

Debug Python in Emacs (last edited 2008-08-01 14:24:15 by c-98-212-25-97)