Brian Goler
Brian Goler

Reputation: 141

Why isn't my webapp2 import / Google App Engine "Hello, World" working?

While I'm able to get my "Hello, World" program running on Google App Engine (GAE), it only works when I create a version that doesn't rely on the webapp2 import. Why isn't the import working? What I need to do to fix it?

Version of helloworld.py that works:

print 'Content-Type: text/plain'
print ''
print 'Hello, World!!'

Version of helloworld.py that does not work:

import webapp2

class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('Hello, World!')

app = webapp2.WSGIApplication([('/', MainPage)], debug=True)

This second version renders as a blank page.

I think the problem is that the webapp2 import isn't working. When I run python from within the same directory as my hello world program from the command line I get the following:

Brians-MacBook-Air-2:app_engine_hello_world brian$ python
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import webapp2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named webapp2

However, I see webapp2.py in the following directory:

Brians-MacBook-Air-2:webapp2 brian$ pwd
/Users/brian/Repos/app_engine_hello_world/build/webapp2

Also, I'm running python 2.7 installed in the following location:

Brians-MacBook-Air-2:app_engine_hello_world brian$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python

EDIT: Adding my app.yaml file & some other potentially useful info...

application: hello-world-cs253
version: 1
runtime: python27
api_version: 1
threadsafe: no

handlers:
- url: /.*
  script: helloworld.py

I'm using version 1.7.0 - 2012-06-26 of the SDK / GAE Launcher

Results are the same in Chrome, Firefox, and Safari

Upvotes: 1

Views: 9230

Answers (2)

Ivan Kalev
Ivan Kalev

Reputation: 11

The problem is within your app.yaml:

  script: helloworld.py

When using the python27 runtime, you must change it to:

  script: helloworld.app

The solution suggested above seems to work, but strictly speaking this is the right way to do it. Using a main function is unnecessary and deprecated.

Upvotes: 1

elssar
elssar

Reputation: 5871

You don't have to install webapp2. When you run the dev_appserver.py with the path to your project directory as the argument, it handles the importing of webapp2.

Do you have a app.yaml in place, and is it correct?

For Python2.7 you need to specify that you're using Python2.7. Runtime should be python27

application: your_app
version: 1
runtime: python27    #important
api_version: 1
threadsafe: true

handlers:
- url: /.*
  script: your_app.py

What command are you running to run your program?

Do this -

you@your-computer:~/GAE_folder$ python dev_appserver.py /path/to/your/project/directory

And then open localhost:8080 on your browser.

EDIT -

I think I know what the problem is. It happened to me too when I first tried out the example given on the site.

Add this bit of code to your helloworld.py file at the end -

def main():
    app.run()

if __name__=='__main__':
    main()

Upvotes: 4

Related Questions