Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I step through/debug a python web application?

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?

like image 371
Blankman Avatar asked Aug 09 '10 18:08

Blankman


2 Answers

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.

like image 164
unutbu Avatar answered Oct 04 '22 04:10

unutbu


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
like image 42
Mark Avatar answered Oct 04 '22 05:10

Mark