5

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?

Mauricio Scheffer
  • 98,863
  • 23
  • 192
  • 275
Blankman
  • 259,732
  • 324
  • 769
  • 1,199

3 Answers3

11

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
  • 842,883
  • 184
  • 1,785
  • 1,677
3

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
Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
Mark
  • 106,305
  • 20
  • 172
  • 230
  • You might also need to enable PythonEnablePdb option as documented in the modpython docs here - http://modpython.org/live/current/doc-html/directives.html#pythonenablepdb – Guruprasad May 29 '14 at 12:19
0

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
  • 2,818
  • 6
  • 32
  • 41