Debugging Python(Twisted) with Emacs¶
Open up your project files. sometimes emacs can’t find them if you don’t have them open before-hand.
Make sure you have a program called
pdbsomewhere in your PATH, with the following contents:
#!/bin/sh exec python -m pdb $1 $2 $3 $4 $5 $6 $7 $8 $9
M-x pdbin emacs. If you usually run your program as
python foo.py, your command line should be
pdb foo.py, for
trialjust add -b to the command line, e.g.:
twistd -b -y my.tac
While pdb waits for your input, go to a place in your code and hit
C-x SPCto insert a break-point. pdb should say something happy. Do this in as many points as you wish.
Go to your pdb buffer and hit
c; this runs as normal until a break-point is found.
Once you get to a breakpoint, use
nto run the current line without stepping through the functions it calls,
wto print out the current stack,
dto go up and down a level in the stack,
p footo print result of expression
Recommendations for effective debugging:
p selfa lot; just knowing the class where the current code is isn’t enough most of the time.
wto get your bearings, it’ll re-display the current-line/arrow
after you use
dand lots more
p selfon the different stack-levels.
If you’ve got a big code-path that you need to grok, keep another buffer open and list the code-path there (e.g., I had a nasty-evil Deferred recursion, and this helped me tons)