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
- Write your program.
Add import pdb
Add pdb.set_trace() wherever you want to set a breakpoint
Run your program: C-c C-c. This should split the screen between your code and the pdb interactive prompt*.
- 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
- Write your program in file 'program.py' with a main function 'main'.
Add pdb.set_trace() wherever you want to set a breakpoint
At the interactive prompt (press C-c C-z to get here), import pdb.
Then import program.
Then pdb.run('program.main()')
PDB takes over the interactive prompt. Press c (continue) to run your program to the first breakpoint. Or single-step with s.
- 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
s : step. Execute the next line of code.
n : next. Execute the next line of code in this function. If the next line of execution would be a function call, the whole function is run and the result returned before you stop again.
p : print. Print the value of some variable.
r : return from function. Run until the current function returns, then resume single-stepping.
c : continue running until next breakpoint. Also used to start your program running.
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
clear n : clear breakpoint at line n.
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
