Saturday, December 28, 2013

Google App Engine - Running GAE inside a virtualenv

To be able to work with Google App Engine inside a virtualenv environment, we have to do some tricks:

1. Download and extract the Google App Engine SDK for Python:

+ Link: https://developers.google.com/appengine/downloads#Google_App_Engine_SDK_for_Python

+ Extract the zip file to /opt directory. So, the directory path for GAE is something like:

/opt/gae


2. Open the activate script of the virtualenv environment at /home/.venv/myenv/bin/activate:

+ Find the line:

PATH="$VIRTUAL_ENV/bin:$PATH"
export PATH



+ Insert the GAE path:

PATH="$VIRTUAL_ENV/bin:$PATH"
PATH="/opt/gae:$PATH"
export PATH


Save & exit

3. Create a path configuration file for GAE inside the virtualenv environment name /home/.venv/myenv/lib/python2.7/site-packages/gae.pth with the following content:

/opt/gae
import dev_appserver; dev_appserver.fix_sys_path()


Now you can run your GAE application inside a virtualenv environment.


If you get trouble with GAE does not regconize any python libraries, modify your app.yaml to list those libraries. For example, if i want to use jinja2 template library, my app.yaml will look something like this:

application: dangtrinhnt
version: 1
runtime: python27
api_version: 1
threadsafe: true
libraries:
- name: jinja2
  version: latest
- name: markupsafe
  version: latest


handlers:
- url: /.*
  script: asciichan.app




References:

[0] http://monocaffe.blogspot.com/2013/04/python-virtualenv-for-google-app-engine.html
[1] http://stackoverflow.com/questions/8108741/google-app-engine-jinja2-importerror-no-module-named-markupsafe