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?
use the logging
module and, of course, print
when on the dev_server.
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.
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.
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.