Skip to main content

Python: Debugging Google App Engine Apps Locally

Python has a wonderful interactive interpreter (i.e. shell). However, sometimes you need more setup before you can start coding. Previously, I wrote Python: Coding in the Debugger for Beginners.

Google App Engine works a bit like CGI in that output to STDOUT goes to the browser. This breaks my normal "import pdb; pdb.set_trace()" trick. However, it's not hard to put STDOUT, etc. back so that you can use pdb in the way you normally would:
for attr in ('stdin', 'stdout', 'stderr'):
setattr(sys, attr, getattr(sys, '__%s__' % attr))
import pdb
pdb.set_trace()

Comments

Ian Bicking said…
Or use weberror's magical new pdb middleware! Well... I haven't tried it with appengine, so there's a decent chance it doesn't work there (but patches welcome).
metapundit.net said…
Heh - I wondered about that today at the hackathon. I tried import pdb;pdb.set_trace() and got a weird error; now I guess i know why...
jjinux said…
> Or use weberror's magical new pdb middleware! Well... I haven't tried it with appengine, so there's a decent chance it doesn't work there (but patches welcome).

Haha, as usual, you're way ahead of me.
Anonymous said…
You can also pass std{in,out} arguments to pdb.Pdb, like so:

def set_trace():
    import pdb, sys
    debugger = pdb.Pdb(stdin=sys.__stdin__,
        stdout=sys.__stdout__)
    debugger.set_trace(sys._getframe().f_back)

This messes with Google's sandbox less.
jjinux said…
> This messes with Google's sandbox less.

Very helpful. Thanks!
Stuart Grimshaw said…
I've tried the various methods mentioned here, but I always get errors displayed in the browser.

Using whit537's method I get this shown in the browser:

BdbQuit at /stop/37023745/
Request Method: GET
Request URL: http://localhost:8080/stop/37023745/
Exception Type: BdbQuit
Exception Value:
Exception Location: /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/bdb.py in dispatch_call, line 80
jjinux said…
It sounds like you're not successfully pointing stdout at the actual terminal. I'm surprised that my technique (in the main body of this blog post) doesn't work.
Stuart Grimshaw said…
Actually, that does look like it works, I just can't work out how to attach to it, I'm running it through the Google App Engine Launcher on my mac if that makes any difference.
jjinux said…
You need to figure out how to start the server from a shell. The start server script (whatever it's called) is on your Mac somewhere ;)
Stuart Grimshaw said…
Turns out "dev_appserver.py" is on the defaultpath, probably set up by the installer, or the "Make Symlinks" option off the gui apps menu.

Go to your apps parent directory and run "dev_appserver.py yourappname"
Jeff Enderwick said…
I tried:
pdb.Pdb(stdin=getattr(sys,'__stdin__'),stdout=getattr(sys,'__stderr__')).set_trace(sys._getframe().f_back)

- the idea being leave stdout alone. Seems to work. I did not have any crap heading back toward the browser. Is there any best soln out there from someone who works on GAE (as a full time thing)?
jjinux said…
> the idea being leave stdout alone. Seems to work.

Thanks.

> Is there any best soln out there from someone who works on GAE (as a full time thing)?

I have no clue.

Popular posts from this blog

Ubuntu 20.04 on a 2015 15" MacBook Pro

I decided to give Ubuntu 20.04 a try on my 2015 15" MacBook Pro. I didn't actually install it; I just live booted from a USB thumb drive which was enough to try out everything I wanted. In summary, it's not perfect, and issues with my camera would prevent me from switching, but given the right hardware, I think it's a really viable option. The first thing I wanted to try was what would happen if I plugged in a non-HiDPI screen given that my laptop has a HiDPI screen. Without sub-pixel scaling, whatever scale rate I picked for one screen would apply to the other. However, once I turned on sub-pixel scaling, I was able to pick different scale rates for the internal and external displays. That looked ok. I tried plugging in and unplugging multiple times, and it didn't crash. I doubt it'd work with my Thunderbolt display at work, but it worked fine for my HDMI displays at home. I even plugged it into my TV, and it stuck to the 100% scaling I picked for the othe

ERNOS: Erlang Networked Operating System

I've been reading Dreaming in Code lately, and I really like it. If you're not a dreamer, you may safely skip the rest of this post ;) In Chapter 10, "Engineers and Artists", Alan Kay, John Backus, and Jaron Lanier really got me thinking. I've also been thinking a lot about Minix 3 , Erlang , and the original Lisp machine . The ideas are beginning to synthesize into something cohesive--more than just the sum of their parts. Now, I'm sure that many of these ideas have already been envisioned within Tunes.org , LLVM , Microsoft's Singularity project, or in some other place that I haven't managed to discover or fully read, but I'm going to blog them anyway. Rather than wax philosophical, let me just dump out some ideas: Start with Minix 3. It's a new microkernel, and it's meant for real use, unlike the original Minix. "This new OS is extremely small, with the part that runs in kernel mode under 4000 lines of executable code.&quo

Haskell or Erlang?

I've coded in both Erlang and Haskell. Erlang is practical, efficient, and useful. It's got a wonderful niche in the distributed world, and it has some real success stories such as CouchDB and jabber.org. Haskell is elegant and beautiful. It's been successful in various programming language competitions. I have some experience in both, but I'm thinking it's time to really commit to learning one of them on a professional level. They both have good books out now, and it's probably time I read one of those books cover to cover. My question is which? Back in 2000, Perl had established a real niche for systems administration, CGI, and text processing. The syntax wasn't exactly beautiful (unless you're into that sort of thing), but it was popular and mature. Python hadn't really become popular, nor did it really have a strong niche (at least as far as I could see). I went with Python because of its elegance, but since then, I've coded both p