views:

169

answers:

4

I'm new to both Python and Google App Engine development and find myself at a loss on how to effectively debug my apps during development. I come from a C#/ASP.NET background where I can typically step through the code in Visual Studio. However, when using a text editor and running dev_appserver.py from a command line, I don't have the luxuries I'm used to. Do any seasoned Python developers have any debugging tips for me?

+3  A: 

use the logging module and, of course, print when on the dev_server.

jldupont
+1  A: 

Spend $35 and get a personal license of Wing IDE. Its debugging capabilities will remind you so much of Visual Studio -- there's even an equivalent of the Immediate window -- that you will get a giant, permanent grin on your face.

Adam Crossland
+2  A: 

You can also force the dev server to drop into pdb by putting bit of code like the following at the place where you'd like to start debugging:

import sys
for attr in ('stdin', 'stdout', 'stderr'):
    setattr(sys, attr, getattr(sys, '__%s__' % attr))
import pdb
pdb.set_trace()

At this point, you can use the various debugger commands to do your debugging. This isn't the most elegant solution, and you'll have to restart your dev server after you get done with pdb, but it works if you really need it.

(Note: In a normal Python environment, import pdb; pdb.set_trace() is usually enough to get yourself into pdb, but when running inside the dev server you have to fix the handles for stdin and friends.)

I should have noted my original source for this tip: http://jjinux.blogspot.com/2008/05/python-debugging-google-app-engine-apps.html

There are a couple more suggestions in the comment thread on that post that might be preferable (in the sense that they monkey around less with the environment that the dev server has created) than the code I posted above.

Will McCutchen
Wow, really neat! I dodn't know how to drop into pdb from the devserver. This is some great advice.
Attila Oláh
@Attila: I edited my answer to make it clear that this advice did not originate with me. But it is a nice trick to have, when you need it.
Will McCutchen
+1  A: 

Use eclipse and PyDev extension. Here is a link to an article on Google about configuring eclipse. You can set break points and inspect live variables, a lot of what your used to in visual studio.

http://code.google.com/appengine/articles/eclipse.html

Jeff Langston