Tuesday, August 24, 2010

Adding Library Paths to Google App Engine Python Projects

Apologies - It has been a while since my last post in this blog. Wedding preparations and a focus on design work for a new version of My Web Brain (along with my day job as a SQL developer on a SAP rollout) has kept me busy.

Did you know you could alter the system path dynamically on Google App Engine Python? This tip is something that I am sure old hands at Python consider basic, but I hadn't seen the technique done before.

As part of some research of the next version of My Web Brain, I evaluated some Google App Engine frameworks, one of which was Tipfy. Tipfy uses this technique front and centre in its main.py file to alter the system path:

import os
import sys

if 'lib' not in sys.path:
    # Add /lib as primary libraries directory, with fallback to /distlib
    # and optionally to distlib loaded using zipimport.
    sys.path[0:0] = ['lib', 'distlib', 'distlib.zip']

Taking control of the system path frees your package structure from the normal constraints of the project folder structure. And as the Tipfy code demonstrates, it also provides a good way of separating framework, vendor and customised code bases with the desired fallback order.

Update 24 Nov 2010: Just a note on what is obvious in hindsight. You need to update sys.path prior to importing from modules on the affected path(s). The same is true for modules with dependencies on the affected path.

No comments:

Post a Comment