NameError: global name 'MainPage' is not defined

253 views
Skip to first unread message

pybuddy

unread,
Apr 17, 2008, 3:10:37 PM4/17/08
to Google App Engine
while trying out the code mentioned in
http://code.google.com/appengine/docs/gettingstarted/usingusers.html
for helloworld.py, the dev_appserver.py starts up but when serving
the
request, it throws followoing stack trace:

Please help!


INFO 2008-04-17 10:30:01,042 appcfg.py] Checking for updates to
the SDK.
INFO 2008-04-17 10:30:01,232 appcfg.py] The SDK is up to date.
WARNING 2008-04-17 10:30:01,233 datastore_file_stub.py] Could not
read datastor
e data from c:\users\vahuja\appdata\local\temp
\dev_appserver.datastore
WARNING 2008-04-17 10:30:01,233 datastore_file_stub.py] Could not
read datastor
e data from c:\users\vahuja\appdata\local\temp
\dev_appserver.datastore.history
INFO 2008-04-17 10:30:01,237 dev_appserver_main.py] Running
application hell
oworld on port 8080: http://localhost:8080
ERROR 2008-04-17 10:30:04,815 dev_appserver.py] Exception
encountered handlin
g request
Traceback (most recent call last):
File "C:\Program Files\Google\google_appengine\google\appengine
\tools
\dev_apps
erver.py", line 2247, in _HandleRequest
base_env_dict=env_dict)
File "C:\Program Files\Google\google_appengine\google\appengine
\tools
\dev_apps
erver.py", line 334, in Dispatch
base_env_dict=base_env_dict)
File "C:\Program Files\Google\google_appengine\google\appengine
\tools
\dev_apps
erver.py", line 1743, in Dispatch
self._module_dict)
File "C:\Program Files\Google\google_appengine\google\appengine
\tools
\dev_apps
erver.py", line 1654, in ExecuteCGI
reset_modules = exec_script(handler_path, cgi_path, hook)
File "C:\Program Files\Google\google_appengine\google\appengine
\tools
\dev_apps
erver.py", line 1555, in ExecuteOrImportScript
exec module_code in script_module.__dict__
File "c:\googapp\helloworld\helloworld.py", line 5, in <module>
class MainPage(webapp.RequestHandler):
File "c:\googapp\helloworld\helloworld.py", line 18, in MainPage
main()
File "c:\googapp\helloworld\helloworld.py", line 15, in main
application = webapp.WSGIApplication([('/', MainPage)],
debug=True)
NameError: global name 'MainPage' is not defined
INFO 2008-04-17 10:30:04,822 dev_appserver.py] "GET / HTTP/1.1"
500 -
----------------------------------------
Exception happened during processing of request from ('127.0.0.1',
53955)
Traceback (most recent call last):
File "C:\Python25\lib\SocketServer.py", line 222, in handle_request
self.process_request(request, client_address)
File "C:\Python25\lib\SocketServer.py", line 241, in
process_request
self.finish_request(request, client_address)
File "C:\Python25\lib\SocketServer.py", line 254, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Program Files\Google\google_appengine\google\appengine
\tools
\dev_apps
erver.py", line 2172, in __init__
BaseHTTPServer.BaseHTTPRequestHandler.__init__(self, *args,
**kwargs)
File "C:\Python25\lib\SocketServer.py", line 522, in __init__
self.handle()
File "C:\Python25\lib\BaseHTTPServer.py", line 316, in handle
self.handle_one_request()
File "C:\Python25\lib\BaseHTTPServer.py", line 310, in
handle_one_request
method()
File "C:\Program Files\Google\google_appengine\google\appengine
\tools
\dev_apps
erver.py", line 2176, in do_GET
self._HandleRequest()
File "C:\Program Files\Google\google_appengine\google\appengine
\tools
\dev_apps
erver.py", line 2266, in _HandleRequest
tbhandler()
File "C:\Python25\lib\cgitb.py", line 290, in handle
self.file.write(doc + '\n')
File "C:\Python25\lib\socket.py", line 262, in write
self.flush()
File "C:\Python25\lib\socket.py", line 249, in flush
self._sock.sendall(buffer)
error: (10054, 'Connection reset by peer')
----------------------------------------


Daniel O'Brien

unread,
Apr 17, 2008, 3:24:27 PM4/17/08
to Google App Engine
Make sure the MainPage class is defined within your code. If you
renamed it at some point while following the example it might explain
why you're seeing this error.

If MainPage is defined, and you're still getting this error, it would
help if you could post the source code for your app.

Daniel

On Apr 17, 12:10 pm, pybuddy <vikas.ah...@gmail.com> wrote:
> while trying out the code mentioned inhttp://code.google.com/appengine/docs/gettingstarted/usingusers.html

pybuddy

unread,
Apr 17, 2008, 3:36:27 PM4/17/08
to Google App Engine
hre's the source code:

import wsgiref.handlers
from google.appengine.api import users
from google.appengine.ext import webapp
class MainPage (webapp.RequestHandler):
def get(self):
user = users.get_current_user()
if user:
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('Hello, ' + user.nickname())
else:
self.redirect(users.create_login_url(self.request.uri))

def main():
application = webapp.WSGIApplication([('/', MainPage)],
debug=True)
wsgiref.handlers.CGIHandler().run(application)
if __name__ == "__main__":
main()
> > ----------------------------------------- Hide quoted text -
>
> - Show quoted text -

Daniel O'Brien

unread,
Apr 17, 2008, 3:52:03 PM4/17/08
to Google App Engine
Either Groups garbled your source, or your indentation is off. That
might explain some of the trouble you're running into. Make sure "def
main():" and "if __name__ ..." aren't indented.

Let me know if you're still getting errors after adjusting the
indentation.

Daniel

pybuddy

unread,
Apr 17, 2008, 4:04:20 PM4/17/08
to Google App Engine
you were right, its the indentation which caused the trouble. the
problem fixed. thanks!
> > > - Show quoted text -- Hide quoted text -

AA

unread,
May 4, 2008, 7:16:25 PM5/4/08
to Google App Engine
I am having a similar problem

Here is my source code - I looked at the indentation but could not see
where the problem would be


import wsgiref.handlers
from google.appengine.api import users
from google.appengine.ext import webapp
class MainPage (webapp.RequestHandler):
def get(self):
user = users.get_current_user()
if user:
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('Hello, ' + user.nickname())
else:
self.redirect(users.create_login_url(self.request.uri))
def main():
application = webapp.WSGIApplication([('/',
MainPage)],debug=True)
wsgiref.handlers.CGIHandler().run(application)
if __name__ == "__main__":
main()


On Apr 17, 4:04 pm, pybuddy <vikas.ah...@gmail.com> wrote:
> you were right, its the indentation which caused the trouble. the
> problem fixed. thanks!
>
> On Apr 17, 12:52 pm, "Daniel O'Brien" <d...@google.com> wrote:
>
>
>
> > Either Groups garbled your source, or your indentation is off. That
> > might explain some of the trouble you're running into. Make sure "def
> > main():" and "if __name__ ..." aren't indented.
>
> > Let me know if you're still getting errors after adjusting the
> > indentation.
>
> > Daniel
>
> > On Apr 17, 12:36 pm, pybuddy <vikas.ah...@gmail.com> wrote:
>
> > > hre's the source code:
>
> > > import wsgiref.handlers
> > > from google.appengine.api import users
> > > from google.appengine.ext import webapp
> > > classMainPage(webapp.RequestHandler):
> > >         def get(self):
> > >                 user = users.get_current_user()
> > >                 if user:
> > >                         self.response.headers['Content-Type'] = 'text/plain'
> > >                         self.response.out.write('Hello, ' + user.nickname())
> > >                 else:
> > >                         self.redirect(users.create_login_url(self.request.uri))
>
> > >         def main():
> > >                 application = webapp.WSGIApplication([('/',MainPage)],
> > > debug=True)
> > >                 wsgiref.handlers.CGIHandler().run(application)
> > >         if __name__ == "__main__":
> > >                 main()
>
> > > On Apr 17, 12:24 pm, "Daniel O'Brien" <d...@google.com> wrote:
>
> > > > Make sure theMainPageclass isdefinedwithin your code. If you
> > > > renamed it at some point while following the example it might explain
> > > > why you're seeing this error.
>
> > > > IfMainPageisdefined, and you're still getting this error, it would
> > > > >     application = webapp.WSGIApplication([('/',MainPage)],

Devraj Mukherjee

unread,
May 4, 2008, 10:14:38 PM5/4/08
to google-a...@googlegroups.com
From what you have copied and pasted, it looks like your def main() is
indented to be part of the class?

Or am I just seeing that wrong because of the copy and paste?

On Mon, May 5, 2008 at 9:16 AM, AA <alnoor....@gmail.com> wrote:
>
> I am having a similar problem
>
> Here is my source code - I looked at the indentation but could not see
> where the problem would be
>
>
> import wsgiref.handlers
> from google.appengine.api import users
> from google.appengine.ext import webapp
> class MainPage (webapp.RequestHandler):
> def get(self):
> user = users.get_current_user()
> if user:
> self.response.headers['Content-Type'] = 'text/plain'
> self.response.out.write('Hello, ' + user.nickname())
> else:
> self.redirect(users.create_login_url(self.request.uri))
> def main():
> application = webapp.WSGIApplication([('/',
> MainPage)],debug=True)
> wsgiref.handlers.CGIHandler().run(application)
> if __name__ == "__main__":
> main()
>

--
"I never look back darling, it distracts from the now", Edna Mode (The
Incredibles)

孙继祖

unread,
Jun 28, 2013, 5:39:15 AM6/28/13
to google-a...@googlegroups.com
Thank you! I'm new to Python, and sometimes forget the importance of indentation. Your kindly reminding really helped me.
Reply all
Reply to author
Forward
0 new messages