GAE& python: getting started
i have just got in touch with python, and am looking for a place to try out some python. i thought of GAE, and tried getting started. this article shares how i got started with GAE with python.
the general process:
- install python on your computer
- install the Python SDK for GAE
- try a local Hello World
- deploy yourHelloWorld to the GAE cloud
what these things mean:
installing python on your computer means to install an interpreter for python programs (or say, python scripts) on your computer (since python is an interpreted language). or say, "open .py files".
the GAE Python SDK is a set of python programs, along with a GUI, with the purpose of simulating the GAE environment on your local computer, and also to conveniently deploy your app to GAE.
setup process details:
install python 2.7 from the python website's download page.
install the GAE Python SDK from the download pageof the GAE site.
now they're set. time to code.
let's code:
no explainations in this section, just instructions.
- create a folder somewhere. this would be your HelloWorld's project folder.
- create two files: "youNameIt.py" and "app.yaml".
- create the contents of the two files as below.
"youNameIt.py":
import webapp2
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Hello, webapp2 World!')
myApp = webapp2.WSGIApplication([('/', MainPage)], debug=True)
note that in the above, "MainPage" don't seem to be alterable.
"app.yaml":
application: youNameIt
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: youNameIt.myApp
that's it.
explanation:
in the above, the "app.yaml" file is a YAML file that describes the structure of the app, and sort of define handlers to requests. the "youNameIt.py" file is the python script that is to be run by the server's web app infrastructure, e.g. webapp2, when a request triggers a handler defined by "app.yaml".
test it locally:
- start the GAE launcher
- add the existing application
- select the item of the youNameIt app in the list
- click run
now visit http://localhost:8080 . hopefully you should see the words "Hello, webapp2 World!" in the browser, indicating an app is now successfully running in your local "GAE emulator".
deploy it:
well, given that you have gone through the above, this is fairly easy.
sign up for a developer account at the Google Developers page. then create a new GAE app in the GAE Admin Console, whose link is on that page. (or if you couldn't find the link i copied it here.)
make sure that the ID (or say, name) of your app is the same in the python script filename, the "app.yaml" content, and the GAE Admin Console. select your app in the list in the GAE launcher window, then click the deploy button. give the password of the Google account which you used to sign up with Google Developers. Google Application Specific Passwords seem to work too.
done.
well, hopefully now you can visit youNameIt.appspot.com and see your Hello World running live.
references:
- Getting Started: Python 2.7 - Google App Engine — Google Developers
- 01 - Google App Engine for Python - Hello World - YouTube
- Google app engine Python hello world example using Eclipse
- Google App Engine with Python 2.7 - CouldNotFindModuleError - Stack Overflow
what could go wrong?
a lot of things went wrong for me through my attempts; i think listing things that went wrong for me may be helpful. for me:
- after i first pressed the run button, the GAE launcher didn't let me run my app (though even locally). later i found out a couple of relevant things that might have caused it:
- which python interpreter installation are you using? i had python 3.2 installed too, and i later found out that python32 kept on being used to run all the scripts, and thus things weren't as expected.
- check your PATH. check that the GAE launcher, and your python installation are both listed.
- i fixed my PATH so that only python27 is included, but when i pressed the run button, the GAE launcher still didn't let me run my app. then i realized that:
- in my "app.yaml" file, after "threadable", i wrote "True". however, what i should have written is "true". i found this out by comparing my code and the code i copy and pasted from Hello, World! - Google App Engine — Google Developers.
- well, soon my app was running locally. but when i tried to deploy it to the cloud, i failed.
- i reckonized that i had an inappropriate global proxy set on my computer. i cancelled the proxy settings, and the uploading went fine.