By default, the App Engine Python 2.7 runtime environment includes the Python standard library, the App Engine libraries, and a few third-party packages. You can add third-party libraries to your app in one of two ways:
- Request the library using the
- The library must be one of the supported requestable libraries.
- App Engine adds the requested libraries to the runtime environment when deployed.
- Install the library in your app root directory.
- The library must be implemented as pure Python code (no C extensions).
- The code is uploaded to App Engine with your application code, and counts toward file quotas.
- Use the vendor module to add the third-party library directory to the load path.
- Requesting Libraries
- Managing Local Libraries with virtualenv
- Adding Third-party Packages to the Application
- Using pip Requirements Files
- Django Notes
- Matplotlib Notes
You can request any of the following libraries via app.yaml:
|Name||Default version||Supported versions||Description|
|A full-featured web application framework for Python.|
|Libraries for building APIs in an App Engine application.|
|A modern and designer friendly templating language for Python.|
|A Pythonic binding for the C libraries libxml2 and libxslt.|
|A XML/HTML/XHTML markup safe string for Python.|
|A 2D plotting library which produces publication-quality figures.|
|A Python DB API v2.0 compatible interface to MySQL.|
|A general-purpose library for array-processing.|
|A library for creating and transforming images.|
|A framework for implementing HTTP-based remote procedure call (RPC) services.|
|A library for generating Cyclic Redundancy Checks (CRC).|
|A library that provides (AMF) Action Message Format functionality.|
|A library of cryptography functions such as random number generation.|
|A library that provides package and module discovery capabilities.|
|The SSL socket wrapper built-in module.|
|A lightweight Python web framework.|
|A library that provides wrappers around the WSGI request environment.|
|A library for YAML serialization and deserialization.|
You request a library using the
libraries: section of
libraries: - name: PIL version: "1.1.7" - name: webob version: "1.1.1"
These libraries are not added to your development environment automatically. You must install them in your development environment to use them with the App Engine development server. A few of these libraries are bundled with the SDK for use by App Engine tools, but in general, you must install them.
$ sudo pip install lxml==2.3.5
Managing Local Libraries with virtualenv
A good way to manage your development environment's libraries is to use virtualenv.
virtualenv creates an environment isolated from other libraries you might have installed on your computer. You can install libraries in the virtual environment to replicate the App Engine runtime environment.
$ sudo pip install virtualenv
Create a virtual environment somewhere outside your application directory. (These files should not be uploaded with your app code.) To create a virtual environment:
$ virtualenv appenv
Activate the virtual environment before starting development. This modified your command environment so that the development server and other tools use the environment. Here is how you activate the environment in a Linux/Mac OS X/Cygwin shell:
$ source appenv/bin/activate
The command prompt changes to indicate that you are now using the virtual environment. You can install libraries into the environment with
$ pip install lxml==2.3.5
With the environment active, you can run
dev_appserver.py as usual. It will use the environment's libraries, and not use other libraries you have installed on your computer.
To deactivate the environment, run the
Adding Third-party Packages to the Application
You can add any third-party library to your application, as long as it is implemented in "pure Python" (no C extensions) and otherwise functions in the App Engine runtime environment. The easiest way to manage this is with a
Create a directory named
lib in your application root directory:
To tell your app how to find libraries in this directory, create (or modify) a file named
appengine_config.py in the root of your project, then add these lines:
from google.appengine.ext import vendor # Add any libraries installed in the "lib" folder. vendor.add('lib')
pip with the
-t lib flag to install libraries in this directory:
$ pip install -t lib gcloud
Note: pip version 6.0.0 or higher is required for
vendor to work properly.
Using pip Requirements Files
pip can read a list of libraries to install from a file, known as a requirements file. Requirements files make it easy to set up a new development environment for your app, and upgrade to new versions of libraries.
A requirements file is a text file with one line per library, listing the package name and version:
Flask==0.10 Markdown==2.5.2 google-api-python-client
To install the libraries from a requirements file, use the
$ pip install -r requirements_cfg.txt
Tip: Use two requirements files, one for managing your app libraries in
lib and another for installing requested libraries in your local environment. Store these files with your code in your source control repository, then use them when you need to set up a new development environment.
$ source appenv/bin/activate (appenv)$ cd myapp (appenv)/myapp$ pip install -r requirements_cfg.txt (appenv)/myapp$ pip install -t lib -r requirements_vnd.txt
Django is a full-featured web application framework for Python. It provides a full stack of interchangable components, including dispatch, views, middleware, and templating components, and many others.
The Django data modeling interface is not compatible with the App Engine datastore. You can use the App Engine data modeling libraries (db or ndb) in your Django applications. However, third-party Django applications that use the Django data modeling interface—most notably Django's Admin application—may not work with App Engine directly.
To use Django with the
NDB storage API, add
MIDDLEWARE_CLASSES entry in your Django
It's best to insert it in front of any other middleware classes,
since some other middleware may make datastore calls and those won't be
handled properly if that middleware is invoked before this middleware.
(You can learn more about
To use Django, specify the WSGI application and Django
... handlers: - url: /.* script: main.app # a WSGI application in the main module's global scope libraries: - name: django version: "1.2"
DJANGO_SETTINGS_MODULE environment variable must be set to the name of your Django settings module, typically
'settings', before packages are imported.
If your Django settings module is something other than
settings.py, set the
variable accordingly either in your
env_variables: DJANGO_SETTINGS_MODULE: 'myapp.settings'
or in your Python code:
import os # specify the name of your settings module os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings' import django.core.handlers.wsgi app = django.core.handlers.wsgi.WSGIHandler()
Note: The experimental release of matplotlib is not supported on the development server. You can still add
matplotlib to the
libraries list, but it will raise an
ImportError exception when imported.
Matplotlib is a plotting library that produces graphs and figures in a variety of image formats. On App Engine, the interactive modes of matplotlib are not supported, and a number of other features are also unavailable. This means you cannot use
pyplot.show() as many matplotlib tutorials suggest. Instead, you should use
pyplot.savefig() to write image data to the output stream, a cStringIO.StringIO instance, or the Blobstore using the Files API.
Matplotlib allows extensive customization through the use of the
matplotlibrc configuration file, which should be placed in the application's top-level directory. Alternatively, you can set the
MATPLOTLIBRC environment variable to a path relative to your application's directory.
The default backend is AGG, which allows writing files of all supported formats: PNG (the default format), RAW, PS, PDF, SVG and SVGZ. If you make the PIL library available by adding
PIL to the
libraries section of
app.yaml, then the AGG backend will automatically support writing JPEG and TIFF image formats as well.
Matplotlib comes with a number of fonts which are automatically available. You can use custom fonts by uploading them (in TTF format) along with your application, and setting the
TTFPATH environment variable to the path where they are located, relative to your application's directory. (See Defining Environment Variables.)
A number of matplotlib features are not supported on App Engine. In particular:
- There is no
~/.matplotlibdirectory (however, there are alternative locations to place the
matplotlibrcconfiguration file, as described above).
- Interactive backends and GUI elements are not supported.
- The EMF, Cairo and GDK backends are not supported.
- There is no caching, and therefore a number of mechanisms will re-calculate or re-download data that would normally be cached. Specific caching mechanisms that have been disabled include font data (calculated by
matplotlib.font_manager.FontManager.findfont), sample data (downloaded by
matplotlib.cbook.get_sample_data) and financial data (downloaded by
- Because there is no caching, it is not possible to call
examples.downloadis set to
- Because there is no caching, it is not possible to call
- All features that invoke external commands have been disabled.
- Use of
fontconfighas been disabled. Fonts are found through the mechanism described above.
- Use of LaTeX for text rendering is not supported. Setting
Truewill not work.
- Use of an external PostScript distiller program is not supported. Setting
xpdfwill not work.
- Use of an external video encoding program is not supported. The
matplotlib.animation.Animation.savemethod will not work, and therefore, the
matplotlib.animationpackage is not useful.
matplotlib.cbook.MemoryMonitorclass are not supported.
- Use of
matplotlib.testfunction has been disabled.
matplotlib.pyplot modules are stateful and not thread-safe. If you use them on App Engine, you must set
threadsafe: false in
app.yaml, and be aware that the plotter state will be preserved between requests on the same instance. For example, you will need to call
pyplot.clf() at the beginning of each request to ensure that previous plots are not visible. It is recommended that you use the thread-safe object-oriented API instead of the stateful pyplot API.