# Using Python 2.7 - Google App Engine - Google Code

English ▼
Skip to page content Skip to main navigation
e.g. "templates" or "datastore"

# Using Python 2.7

This page provides installation and migration instructions for Python 2.7. You can use the information here to configure a new Python 2.7 application or update an existing application. The page has the following sections:

- Prerequisites
- Configuring the Application
- Configuring Libraries
- Configuring WSGI Script Handlers
- Multithreading
- Sandboxing
- Uploading the Application
- Considerations when Migrating Your App

## Prerequisites

In order to use Python 2.7, an application must meet the following requirements:

- The application must use the High Replication datastore (HRD).
- If the application uses Django, it must use version 1.2. See the Django documentation for upgrade details.
- If you wish to use concurrent requests, the application must use WSGI script handlers.

## Configuring the Application

Python 2.7 requires a special `runtime` configuration element in the header of `app.yaml`. Note that the `threadsafe:` [`true` | `false`] element is required for Python 2.7 applications. If `true`, App Engine sends requests concurrently; if `false`, App Engine sends them serially. The following `app.yaml` header enables concurrent requests:

    application: myapp
    version: 1
    runtime: python27
    api_version: 1
    threadsafe: true
    ...

## Configuring Libraries

The Python 2.7 runtime includes some additional third-party modules and makes them available on demand. Other libraries, such as WebOb, are available by default, but you can specify which version you want to use. To specify the latest version, add `version: latest` to `app.yaml`:

    libraries:
    - name: PIL
      version: latest
    - name: webob
      version: latest

In some cases, new libraries are not backward compatible. To avoid compatibility issues with the new libraries, you can specify an exact version:

    libraries:
    - name: PIL
      version: "1.1.7"

Python 2.7 supports the following combinations of names and versions:

Name | Default | Supported Versions
--- | --- | ---
django | Not Available | "1.2"
jinja2 | Not Available | "2.6"
lxml [1] | Not Available | "2.3"
markupsafe [1] | Not Available | "0.15"
numpy [1] | Not Available | "1.6.1"
PIL [1] | Not Available | "1.1.7"
pycrypto [1] | Not Available | "2.3"
setuptools | Not Available | "0.6c11"
webapp2 | 2.3 | "2.3"
webob | 1.1.1 | "1.1.1"
yaml | 3.10 | "3.10"

[1] To test applications using this library with the development web server, you must download and install it on your local machine.

## Configuring WSGI Script Handlers

The Python 2.7 runtime supports Web Server Gateway Interface (WSGI) applications natively. Instead of specifying a CGI handler (i.e. a Python script) in `app.yaml` and running your web application in your script, you can specify the application directly:

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

You also need to move your WSGI application object to the global scope:

    import webapp2

    class MainPage(webapp2.RequestHandler):
        def get(self):
            self.response.headers['Content-Type'] = 'text/plain'
            self.response.out.write('Hello, WebApp World!')

    app = webapp2.WSGIApplication([('/', MainPage)])

You can still specify CGI script handlers in `app.yaml`.

Warning! If you plan to use concurrent requests, you must use WSGI handlers.

## Multithreading

To use Python 2.7, you must specify a `threadsafe` element in `app.yaml`. If `true`, App Engine sends requests concurrently; if `false`, App Engine sends them serially.

    application: myapp
    version: 1
    runtime: python27
    api_version: 1
    threadsafe: true

## Sandboxing

The Python 2.7 runtime no longer restricts access to Python bytecode. Libraries that generate or manipulate bytecode (such as the jinja2 templating library) can do so in this runtime.

You can upload and use `.pyc`, but not in combination with `.py` files. You can upload `.zip` files containing `.py` or `.pyc` files (or a combination).

Threads can be created using the thread or threading modules. Note that threads will be joined by the runtime when the request ends, so the threads cannot run past the end of the request.

## Uploading the Application

After configuring and updating the application, upload it using `appcfg.py`:

    % appcfg.py update myapp

Once you've uploaded the application, you need to test it extensively to ensure backward compatibility. If you are experiencing issues, see the Considerations when Migrating Your App section below, or check out the forums.

## Considerations when Migrating Your App

In Python 2.7, you must use specific versions of some App Engine features and third-party libraries. You will encounter backward-compatibility issues unless you test and update the application.

The following list identifies the primary compatibility issues and points to further resources to resolve them. Google recommends testing the application extensively after upgrading any of the following libraries:

- App Engine Datastore: the application must use the High Replication datastore. If you are currently using the Master/Slave datastore, you need to migrate your app before upgrading to Python 2.7.
- 32-bit mode: The Python 2.7 runtime supports only 32-bit mode. If you have code that relies on 64-bit functionality, you need to change it prior to migrating.
- CGI vs. WSGI: If you want to use concurrent requests, the application must configure script handlers using WSGI, not CGI.
- Latency: In some circumstances, applications may experience increased latency in Python 2.7. If you experience increased latency and it's an issue for your application, you can increase the frontend instance class and enable threadsafe in `app.yaml`.
- Django: You must use Django 1.2 with Python 2.7. For more information, see Porting Your Apps from Django .96 to 1.0.
- PyCrypto: `Crypto.Util.randpool` has been deprecated in favor of `Crypto.Random`. For more information, see What to do about RandomPool.
- PyYaml: PyYaml now uses version 3.1.0. For differences between these two versions, see PyYaml Version History.
- WebApp: WebApp templates are deprecated in Python 2.7. Instead, you can use Django templates directly, jinja2, or a different template engine of your choice.
- WebOb: Python 2.7 supports WebOb version 1.1. This version is not fully backward compatible with the old version (0.9). If the application uses WebOb, you need to test it extensively to catch any errors resulting from the upgrade.
- zipimport: Python 2.7 does not support zipimport, but Python 2.7 can natively import from .zip files.