Debugging TurboGears

Posted by toddw on 2007-03-08 19:26

A number of people saw the debugging Django post and suggested I'd write one covering TurboGears as well. Great idea, so here goes...

I found the best approach for this was to use the remote debugging method:

  • Edit your TurboGears main file start file "" and
    add the following lines, ensuring they are the first 2 code lines in the file:
    import dbgp.client
    dbgp.client.brk(host="localhost", port=9000)

    Note: Change "localhost" to be the machine Komodo is running on (it can be on a different machine) and change 9000 to be the debug port Komodo is set to listen to, you can find this debugger port by looking at Komodo's menu "Debug->Listener Status".

    This ensures that Komodo's dbgp debugging client gets properly initialized and any subsequent threads will get properly debugged by Komodo.

  • Add breakpoints to your python code like this:
    from dbgp.client import brk # Import Komodo debugger brk function
    import time
    from turbogears import controllers, expose, flash

    class Root(controllers.RootController):
        def index(self):
            brk()    # Make Komodo debugging break here
            return dict(now=time.ctime())

  • Ensure Komodo's python dbgp is on your pythonpath, and then start your server:
    export PYTHONPATH=/path-to-komodo-install/lib/support/dbgp/pythonlib
    Note: Change "path-to-komodo-install" to be you Komodo install location.
  • Run your TurboGears server:

Some notes on TurboGears and autoreload:

  • If you are using the auto-reload feature (you likely are as it's turned on by default in the dev version), you'll initially get 2 debug sessions connect to Komodo. The first is for the reloader and the subsequent one(s) are for the server.
  • I say subsequent one(s) because everytime you change a controller file, the server will reload, it does this by shutting down the old process and creating a new process, so this initiates a new debug session to Komodo!
  • For debugging purposes, I suggest turning off this auto-reload functionality by setting the following in your TurboGears config file (i.e. dev.cfg):