Skip to content

Instantly share code, notes, and snippets.

@techniq
Last active December 11, 2015 17:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save techniq/4632192 to your computer and use it in GitHub Desktop.
Save techniq/4632192 to your computer and use it in GitHub Desktop.
Path fixer for Google App Engine to load libraries from packages directory.

path_fixer.py should be placed at the root of your project, and imported at the top of any of your entry point scripts (main.py, manage.py, test.py, etc)

All your libraries should be copied from site-packages (ex. /Library/Python/2.7/site-packages or your virtualenv) and into a packages directory at the root of your project.

myapp
|-- packages
   |-- library1.py
   |-- library2.py
   |-- library3
      | -- __init__.py
      | -- library3.py
|-- app.yaml
|-- main.py
|-- path_fixer.py
application: myapp
version: 1
runtime: python27
api_version: 1
threadsafe: true
default_expiration: "7d"
handlers:
- url: /favicon.ico
static_files: application/static/img/favicon.ico
upload: application/static/images/favicon.ico
- url: /robots.txt
static_files: application/static/robots.txt
upload: application/static/robots.txt
- url: /static
static_dir: application/static
- url: .*
script: main.app
import path_fixer
import webapp2
import routes
import config
app = webapp2.WSGIApplication(config=config.webapp2_config)
routes.add_routes(app)
import os
import sys
package_dirs = ["packages"]
for package_dir in package_dirs:
if os.path.exists(package_dir):
package_dir_path = os.path.join(os.path.dirname(__file__), package_dir)
sys.path.insert(0, package_dir_path)
# Append zip archives to path for zipimport
for filename in os.listdir(package_dir_path):
if filename.endswith((".zip", ".egg")):
sys.path.insert(0, "%s/%s" % (package_dir_path, filename))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment