Inconsistent behaviour when if __name__ == '__main__' missing

6 views
Skip to first unread message

mrts

unread,
Dec 13, 2008, 10:45:41 AM12/13/08
to Google App Engine
This is really a doc bug report, but perhaps it should be discussed
first.

The problem:

Given

def main():
run_wsgi_app(handler)

if

if __name__ == '__main__':
main()

is omitted from the end of main.py, the application serves a blank
page (i.e. null body) on first request and sporadically later as well
(probably when the main module is imported in another server in the
cloud, i.e. main() is *not* run only on import). Otherwise, main() is
run automagically and everything works as expected.

This is highly confusing and hard to track down -- so it would make
sense to either not run main() automagically at all so that zero
length response is always returned if no __name__ == '__main__' clause
is present, or document this behaviour with a big warning in the
official docs.

At least two people have tripped on this as of now.

Jeff S

unread,
Dec 15, 2008, 1:49:03 PM12/15/08
to Google App Engine
Thank you for pointing this out. It seems like most code snippets do
include the if __name__ == '__main__' but I did find one that omitted
it

http://code.google.com/appengine/docs/webapp/running.html

Were there others that you had seen?

Thank you,

Jeff

Dan Sanderson

unread,
Dec 16, 2008, 2:40:43 PM12/16/08
to google-a...@googlegroups.com
Good catch!  I'll make sure the docs address this case, in both samples and descriptions.

Thanks!
-- Dan

SM

unread,
Jan 8, 2009, 12:31:34 AM1/8/09
to Google App Engine
Can someone expound on this a bit more?

Is

if __name__ == '__main__': main()

required for App Engine to work properly? Or is it sufficient to
simply define a main() function in your script and App Engine will
call it appropriately?

Thanks.

On Dec 16 2008, 11:40 am, Dan Sanderson <dansander...@google.com>
wrote:
Reply all
Reply to author
Forward
0 new messages