Works+Interactive > Blog > Using Bottle with Python2.7 on Google App Engine

Using Bottle with Python2.7 on Google App Engine

Posted by Peter on May 24, 2012

Want to use Google App Engine's brand spanking new Python2.7 runtime with BottlePy, but can't get the bottlepy.org's tutorial code to work? Worry not, here's a fool-proof way to get your Bottle powered web app up and running on GAE using Python2.7 in no time :)!

This tutorial assumes you've already obtained the Google App Engine's latest SDK (if not, grab it from here) and got a copy of Bottle installed, at least version 0.09 (I'm using 0.11-dev, at the time of writing, but the differences should not affect this tutorial if you choose to use 0.09-old or 0.10-stable).

If you follow the deployment reciple for GAE on Bottlepy.org, you'll notice that it doesn't work for Python2.7. The reason is because for Python2.7, you need to expose a WSGI app handler and you need to specify such in your app.yaml.

Assume you have a main.py and app.yaml file as such:

main.py

import bottle
from bottle import *

@get('/')
def homeGET():
    return "Look mah, Python 2.7 with no training wheels!"

bottle.run(server='gae')

app.yaml

application: MyBottleApp
version: 1
runtime: python27
api_version: 1
threadsafe: yes

handlers:
- url: /static
  static_dir: static

- url: /.*
  script: main.py


This will not work. Instead, you need to wrap your code in a custom Bottle object, which we can then pass it onto the WSGI app handler in our updated app.yaml. Observe the following main.py and app.yaml files:

main.py

import bottle
from bottle import *

app = Bottle()

@app.get('/')
def homeGET():
    return "Look mah, Python 2.7 with no training wheels!"

bottle.run(app=app, server='gae')

app.yaml

application: MyBottleApp
version: 1
runtime: python27
api_version: 1
threadsafe: yes

handlers:
- url: /static
  static_dir: static

- url: /.*
  script: main.app


Note how we've created our custome Bottle object instead of relying on the default bottle object and attach all our routes and handlers to this object, and then passed this object we created when we call "bottle.run()".

We also made a minor edit to app.yaml. The url handler script we are passing is main.app, where 'app' is our Bottle object which we passed to the WSGI app handler.

Tada! A Bottle app running on GAE's Python2.7 runtime.

I hope this was easy and painless enough. If you have any questions, don't hesitate to ask!

Comments:

Posted by myke on
Hi,I've tried the steps but it seems to work only the first time the page is loaded.on refreshing I get error. __call__() takes only 1 argument 3 given. But on editing main.app to main.py it works fine why is this so?
Posted by Peter on
The reason why is because you are wrapping the main.py app as a WSGI app, and not main.app.

This is why in my post I mentioned that it *will* work if the script target is main.py, but not main.app as specified in the tutorials.

Cheers (and sorry for the delayed reply)!
Posted by Selma on
Thank you very much!
Posted by Nielson on
Thanks for this tutorial!!
Posted by Udi on
Hi!

You can drop the bottle.run() line,
app engine will detect the "app" variable on your main.py and will use it.

Udi
Posted by Kellen Green on
Here's my GAE w/Python 2.7 and bottle go to, should anyone else stumble upon this article.

from bottle import Bottle, debug
app = Bottle()
if DEBUG:
debug(True)
else:
from application import errors
from application import views
Leave a Reply



(Your email will not be publicly displayed.)

Please type the letters and numbers shown in the image.Captcha Code