简体   繁体   中英

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?

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.

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

use Python Debbuger, import pdb; pdb.set_trace() 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM