views:

119

answers:

3

I can't seem to find any information on debugging a python web application, specifically stepping through the execution of a web request.

is this just not possible? if no, why not?

+3  A: 

If you put

import pdb
pdb.set_trace()

in your code, the web app will drop to a pdb debugger session upon executing set_trace.

Also useful, is

import code
code.interact(local=locals())

which drops you to the python interpreter. Pressing Ctrl-d resumes execution.

Still more useful, is

import IPython.Shell 
ipshell = IPython.Shell.IPShellEmbed()
ipshell(local_ns=locals())

which drops you into an IPython session (assuming you've installed IPython). Here too, pressing Ctrl-d resumes execution.

unutbu
A: 

use Python Debbuger, import pdb; pdb.set_trace() exactly where you want to start debugging, and your terminal will pause in that line. More info here: http://plone.org/documentation/kb/using-pdb

juanefren
+1  A: 

If you are running your web application through apache and mod_wsgi or mod_python, both provide some support for step through debugging with pdb. The trick is you have to run apache in foreground mode with the -X flag.

On my Gentoo system I do this with (this is essentially the same command the apache init script uses replacing the -k start with the -X):

/usr/sbin/apache2 -D DEFAULT_VHOST -D INFO -D LANGUAGE -D SSL -D SSL_DEFAULT_VHOST -D PYTHON -d /usr/lib64/apache2 -f /etc/apache2/httpd.conf -X
Mark