My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
PyramidTutorial  
Example setup running pyramid on appengine.
pyramid, appengine, python, tutorial
Updated Dec 17, 2010 by zutes...@gmail.com

Introduction

This is a tutorial on getting the basic tutorial code for Pyramid running on appengine. Its based on predefined project. A paster based template to build the project will come later.

Details

This setup assumes your using linux. Code and buildout config for the tutorial is found at http://bfg-pages.googlecode.com/svn/pyramid-gae-tutorial/trunk

Setup

To do the setup I am going to use a buildout recipe developed by Rodrigo Moraes <rodrigo moraes at gmail com>. Recipe at http://pypi.python.org/pypi/appfy.recipe.gae/0.9.1.

Make a directory for your tutorial.

timh@chrome:~$ mkdir tut1
timh@chrome:~$ cd tut1

Then setup virtualenv so you can install the buildout recipe.

timh@chrome:~/tut1$ virtualenv --no-site-packages --python=/usr/bin/python2.5 .
Running virtualenv with interpreter /usr/bin/python2.5
New python executable in ./bin/python2.5
Also creating executable in ./bin/python
Installing setuptools............done.

Don't activate the virtualenv. Use explicit paths when referencing scripts set up by virtualenv.

Now use easy_install to install the appfy.recipe.gae

timh@chrome:~/tut1$ ./bin/easy_install appfy.recipe.gae

Next check out the sample app which has the bootstrap and buildout configs to be used for the rest of the tutorial.

timh@chrome:~/tut1$
timh@chrome:~/tut1$ svn checkout http://bfg-pages.googlecode.com/svn/pyramid-gae-tutorial/trunk app
A    app/default_error.html
A    app/settings.yaml
A    app/app.yaml
A    app/pkg_resources.py
A    app/views.py
A    app/bootstrap.py
A    app/buildout.cfg
A    app/lib
A    app/lib/dist
A    app/utils.py
A    app/static
A    app/static/logo.png
A    app/static/pylons.css
A    app/static/favicon.ico
A    app/tests.py
A    app/models.py
A    app/main.py
A    app/templates
A    app/templates/mytemplate.pt
Checked out revision 159.

Next we need to create some symlinks to varios .cfg files required for buildout.

timh@chrome:~/tut1$ ln -s app/buildout.cfg 
timh@chrome:~/tut1$ ln -s app/bootstrap.py 
timh@chrome:~/tut1$ ln -s app/gaetools.cfg 
timh@chrome:~/tut1$ ln -s app/versions.cfg 

make a var/downloads so that buildout can cache its downloads.

timh@chrome:~/tut1$ mkdir -p var/downloads

Then run bootstrap.py which sets up the environment (this will use distribute)

timh@chrome:~/tut1$ ./bin/python bootstrap.py 

... stuff happens ...

Now run buildout, which will download and install pyramid and all of it's dependancies in app/lib/dist. This recipe unpicks the eggs and only deploys the required files.

timh@chrome:~/tut1$ ./bin/buildout

... stuff happens ...

timh@chrome:~/tut1$

Now start the dev server.

timh@chrome:~/tut1$ bin/dev_appserver app
INFO     2010-12-03 15:24:27,784 appengine_rpc.py:153] Server: appengine.google.com
WARNING  2010-12-03 15:24:27,788 datastore_file_stub.py:573] Could not read datastore data from /home/timh/tut1/var/data.store
INFO     2010-12-03 15:24:27,816 dev_appserver_main.py:485] Running application pyramid-gae-tutorial on port 8080: http://localhost:8080
WARNING  2010-12-03 15:24:33,924 main.py:20] Starting main

Point your web browser at http://localhost:8080/ and something similar to below will appear in the terminal

INFO     2010-12-03 15:24:34,171 dev_appserver.py:3317] "GET / HTTP/1.1" 200 -

This sample app has been deployed on appspot. You can see it at http://pyramid-gae-tutorial.appspot.com/

Details of the sample app

Now lets look at the difference between this sample app and the one in the original tutorial.

things to discuss.

whats in the app

   + app
      + lib
         + dist
            #unpicked pyramid and it's dependancies
            + chameleon  
            + mako  
            + markupsafe  
            + paste  
            + pyramid  
            + README.txt  
            + repoze  
            + simplejson  
            + tests  
            + translationstring  
            + venusian  
            + webob  
            + zope
 
      app.yaml
      default_error.html
      main.py
      pkg_resources.py
      + static
         favicon.ico  
         logo.png  
         pylons.css

      + templates
          mytemplate.pt

      tests.py
      views.py

You will also note there are the .cfg files that you created the symlink to eariler. These are not part of the appengine application. (I need to stick them somewhere else ;-)

The contents of lib are as a result of running buildout, which installs pyramid from PyPI eggs and all of its dependancies and then proceeds to copy the actual code from the eggs to lib/dist (I need to move everything up to dist, thats a hold over from the original recipe for tipfy, which I am leveraging off)

main.py

import logging

logging.getLogger().setLevel(logging.INFO)

import sys,os

sys.path.insert(0,'lib/dist')

from pyramid.configuration import Configurator
from models import get_root
from google.appengine.ext.webapp.util import run_wsgi_app

settings = {
    'reload_templates': 'false',
    'debug_authorization': 'false',
    'debug_notfound': 'false',
    'debug_templates': 'false',
    'default_locale_name': 'en',
}

def main():
    """ This function runs a Pyramid WSGI application.
    """
    
    config = Configurator(root_factory=get_root,settings=settings)
    config.add_view('views.my_view',
                    context='models.MyModel',
                    renderer='templates/mytemplate.pt')
    app = config.make_wsgi_app()
    run_wsgi_app(app)
            
if __name__ == '__main__':
  main()
  • run_wsgi_app
  • settings
  • debugging with pdb
  • static handlers.
  • default_error handler
  • app.yaml
  • configuring chameleon

Deploying the app

  • appcfg

Running tests

put stuff here ...

Some important info/links for appengine

Import things to note

here are some random things a pyramid developer new to appengine needs to know.

  • appengine runs python 2.5 in production. Do not be even tempted to run python 2.6 or later. You have been warned ;-)
  • appengine runs multiple single threaded instances of your code, to service the requests.
  • instances of your app can only share state/information via the datastore or memcache.
  • web requests must run within 30seconds.
  • startup time for a cold instance is crucial.
    • Try and defer things if you can. For instance don't load up a deform/formish schemas for forms until you actually need them.
    • do all you configuration imperatively. zcml and scan is slower.
  • You can not write to the filesystem in appengine.

Important links to documents and/or resources

Comment by charlie....@clark-consulting.eu, Dec 6, 2010

No need to use activate and bin/easy_install. Better not to use activate and use local paths instead (consistently), i.e. also when running buildout.

Comment by rich.chu...@gmail.com, Dec 11, 2010

Note that when starting the development server, bin/dev_appserver should be bin/dev_appserver app.

Comment by project member zutes...@gmail.com, Dec 17, 2010

Hi Rich.

At least in my install the "app" bit is optional. Though I would say makes things a little clearer.

Comment by project member zutes...@gmail.com, Dec 17, 2010

Hi Charlie. I have made you suggested changes. It's probably a good idea for people to not get in the habit activating ;-)

Rich, I have changed the bin/dev_appserver to bin/dev_appserver app

Comment by charlie....@clark-consulting.eu, Dec 17, 2010

Tim, you've been to judicious with the scalpel! It still says "Activate the virtualenv".

Comment by project member zutes...@gmail.com, Dec 17, 2010

Oops, yep missed that one, thanks

Comment by bshan...@gmail.com, Jan 27, 2011

i got an error upon ./bin/buildout:

$ ./bin/buildout Updating gae_sdk. Installing gae_tools. While:

Installing gae_tools.

An internal error occurred due to a bug in either zc.buildout or in a recipe being used: Traceback (most recent call last):

File "/home/bshanks/prog/myapp/lib/python2.5/site-packages/zc.buildout-1.5.2-py2.5.egg/zc/buildout/buildout.py", line 1805, in main
getattr(buildout, command)(args)
File "/home/bshanks/prog/myapp/lib/python2.5/site-packages/zc.buildout-1.5.2-py2.5.egg/zc/buildout/buildout.py", line 584, in install
installed_files = selfpart?.call(recipe.install)
File "/home/bshanks/prog/myapp/lib/python2.5/site-packages/zc.buildout-1.5.2-py2.5.egg/zc/buildout/buildout.py", line 1297, in call
return f()
File "/home/bshanks/prog/myapp/lib/python2.5/site-packages/appfy.recipe.gae-0.9.3-py2.5.egg/appfy/recipe/gae/tools.py", line 143, in install
return super(Recipe, self).install()
File "/home/bshanks/prog/myapp/lib/python2.5/site-packages/zc.recipe.egg-1.3.2-py2.5.egg/zc/recipe/egg/egg.py", line 173, in install
return self.install(reqs, ws, scripts)
File "/home/bshanks/prog/myapp/lib/python2.5/site-packages/zc.recipe.egg-1.3.2-py2.5.egg/zc/recipe/egg/egg.py", line 195, in install
relative_paths=self.relative_paths
File "/home/bshanks/prog/myapp/lib/python2.5/site-packages/zc.buildout-1.5.2-py2.5.egg/zc/buildout/easy_install.py", line 1233, in scripts
pyscript(spath, sname, executable, rpsetup))
File "/home/bshanks/prog/myapp/lib/python2.5/site-packages/zc.buildout-1.5.2-py2.5.egg/zc/buildout/easy_install.py", line 1528, in pyscript
return write_script(dest, contents, 'interpreter')
File "/home/bshanks/prog/myapp/lib/python2.5/site-packages/zc.buildout-1.5.2-py2.5.egg/zc/buildout/easy_install.py", line 1460, in write_script
open(script_name, 'w').write(contents)
IOError: 26? Text file busy: '/home/bshanks/prog/myapp/bin/python'

Comment by bshan...@gmail.com, Jan 27, 2011

doing this instead works (putting the virtualenv in ./python instead of . ):

mkdir pyramid2

cd pyramid2

virtualenv --no-site-packages --python=/usr/bin/python2.5 python

python/bin/easy_install appfy.recipe.gae

svn checkout http://bfg-pages.googlecode.com/svn/pyramid-gae-tutorial/trunk app

ln -s app/versions.cfg

ln -s app/gaetools.cfg

ln -s app/buildout.cfg

ln -s app/bootstrap.py

mkdir -p var/downloads

python/bin/python bootstrap.py

python/bin/buildout

#to run:

bin/dev_appserver app

#to deploy:

bin/appcfg update app

Comment by bshan...@gmail.com, Jan 27, 2011

however, how is one supposed to do this on Windows? does one just use "mlink" instead of "ln"?

Comment by project member zutes...@gmail.com, Feb 3, 2011

Sorry I don't do any development in windows. I would just copy those files rather than linking them

Comment by rich.chu...@gmail.com, Feb 12, 2011

bshanks3: I've never gotten Pyramid and GAE to work under Windows. Let me know if you have a reliable method! I use VirtualBox? to run Ubuntu under Windows where necessary.

Tim: you might be interested to know you inspired me to write a very basic tutorial based in part on your work here, after you mentioned that it was possible to deploy to App Engine without using Paster in an IRC conversation a few months back. It's up at http://www.eloquentgeek.com. I'm hoping to develop the blog it uses as a test case into something a bit more functional. I'd welcome your suggestions and corrections. It doesn't have a comment facility yet though!

Comment by project member zutes...@gmail.com, Feb 15, 2011

rich: cool. Glad to see some other people starting to look at making appengine/pyramid an easier route to take.

Comment by bayle.shanks@gmail.com, Mar 19, 2011

Now I get this error:

Getting distribution for 'Chameleon>=1.2.3'. The required version of distribute (>=0.6.14) is not available, and can't be installed while this script is running. Please install a more recent version first, using 'easy_install -U distribute'.

(Currently using distribute 0.6.10 (/home/bshanks/prog/pietrust/pie-share-pyramid/python/lib/python2.5/site-packages/distribute-0.6.10-py2.5.egg)) error: Setup script exited with 2 An error occurred when trying to install Chameleon 2.0-rc6. Look above this message for any errors that were output by easy_install. While:

Installing app_lib. Getting distribution for 'Chameleon>=1.2.3'.
Error: Couldn't install: Chameleon 2.0-rc6

Comment by mwhip...@mattwhipple.com, Apr 9, 2011

None of the buildout approaches have worked for me out of the box, but I finally just took the time to hack this one into submission. A more refined explanation will follow but one thing that absorbed far too much time was a pkg_resources loader error which was ultimately solved by the following block stolen from appengine_monkey (add it to main.py):

try:
    import pkg_resources
except ImportError:
    pass
else:
    if hasattr(os, '__loader__'):
        # This only seems to apply to the SDK
        pkg_resources.register_loader_type(type(os.__loader__), pkg_resources.DefaultProvider)
Comment by whippl...@gmail.com, Apr 11, 2011

The modifications that I've had to make to this are outlined here:

http://whippleit.blogspot.com/2011/04/pyramid-on-google-app-engine-take-1-for.html

Comment by Kharchen...@gmail.com, May 24 (2 days ago)

I'm getting this error when running buildout.

RuntimeError?: On Python 2, Pyramid requires Python 2.6 or better An error occurred when trying to install pyramid 1.3.2. Look above this message for any errors that were output by easy_install. While:

Installing app_lib. Getting distribution for 'pyramid'.
Error: Couldn't install: pyramid 1.3.2


Sign in to add a comment
Powered by Google Project Hosting