Johntron

Python, PHP, Zend, Django, Mobile, and Linux web development guru



Interactively debug Django applications


Quit banging your head and learn to debug your Python web application using an interactive stack trace. If you use Apache with mod_wsgi to host a Python application, it is possible to use the Python Debugger (pdb) to trace your code through the console. I use this three-step method to debug my Django projects. I thought about using the Django Debug Toolbar, but this can only be used in HTML responses. For instance, JSON, XML, or Plaintext responses cannot be debugged with the toolbar.

Step one: Wrap your WSGI application in a Debugger class

Edit your WSGI script (specified in your Apache configuration file) to wrap your WSGI application in a Debugger class.

import os
import sys

sys.path.append( '/www/yourproject' )
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
import settings

if settings.INTERACTIVE_DEBUG:
	class Debugger:

	    def __init__(self, object):
	        self.__object = object

	    def __call__(self, *args, **kwargs):
	        import pdb, sys
	        debugger = pdb.Pdb()
	        debugger.use_rawinput = 0
	        debugger.reset()
	        sys.settrace(debugger.trace_dispatch)

	        try:
	            return self.__object(*args, **kwargs)
	        finally:
	            debugger.quitting = 1
            sys.settrace(None)

	application = Debugger( django.core.handlers.wsgi.WSGIHandler() )
else:
	application = django.core.handlers.wsgi.WSGIHandler()

See modwsgi Debugging Techniques (and scroll down quite a bit) for more information.

Step two: Add INTERACTIVE_DEBUG to settings

In your settings.py file, add the following just below DEBUG = True:
INTERACTIVE_DEBUG = True

Step two: Restart Apache in interactive mode

In your console (as root) type the following:

apache2ctl stop
apache2ctl -X

This should appear to hang. It’s actually just waiting for you to load a page. Move on.

Step three: Request a webpage

Now, just request a webpage from your server like normal and notice your browser hanging now as well. This is because the Python Debugger (pdb) is waiting for input from you. If you switch back to your console, you should see a pdb prompt. From this point on, you can issue pdb commands in the interactive pdb prompt like you would for any Python application. See pdb commands for more info.

Here’s a list of the most common commands:

  • b myproject/app/file.py:23 – Set breakpoint on line 23 of myproject/app/file.py (should be in your python path)
  • c – Continue and stop at any breakpoints you’ve set
  • w – Show backtrace to current position
  • l – List sourcecode in current file
  • s – Continue and stop at next possible occasion
  • n – Continue and stop at next line of current function or when function returns
  • print var – Print’s python variable var

In addition to these commands, you can also run any Python code as long as they don’t conflict with these commands.

If this helps, be sure to leave a comment. Thanks!

Updated 23 Nov 2010: Added INTERACTIVE_DEBUG feature.



Tags: , , ,