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