Sep 5, 2010

Best practice to import libraries or frameworks in App engine

Usual procedure is to paste the library in your application's path then upload it with your application using appcfg - update.
This works but sometimes we have to deal with huge libraries consisting of hundreds of python files that sometimes exceed the upper limit of 1000 files / application limit or reduce the number of available files for your application. Also maintaining  those files becomes a nightmare.
So what is the proper way ? :
Well Python's import works straight from the box with a zip file as well, if it finds it your sys.path it treats it as a directory. (for more complicated situations you can look at zipimport module)
Compress the library in a zip file.
You can optionally  include an empty   ""  in each directory (if not there already) so it can be treated as a normal python package.
Paste just the zip file in your application - or better put it somewhere and only paste a link to this file in your applications directory (this way you can maintain the library in a single place and use it on many applications)
Then whenever you want to import from that library you can use something like :
import sys
import os
myLibrary= os.path.dirname(__file__)+'/'
#(os.path.dirname(__file__)  gives you the directory of currently executing script)
if not mylibrary in sys.path:sys.path.insert(0, mylibrary)
#(or sys.path.append(mylibrary)  
import mylibrary
Thats it !

No comments:

Post a Comment