★ wanayoo — archive 1999 https://developers.google.com/appengine/docs/python/python27/using27Nouvelle recherche | Portail wanayoo

Google App Engine







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:

  1. Prerequisites
  2. Configuring the Application
  3. Configuring Libraries
  4. Configuring WSGI Script Handlers
  5. Multithreading
  6. Sandboxing
  7. Uploading the Application
  8. Considerations When Migrating Your Application

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 third-party modules. Some of these are available by default; others are only available if configured. You can specify which version you want to use.

libraries:
- name: PIL
  version: "1.1.7"
- name: webob
  version: "1.1.1"

You can specify that the application should use the latest version of the module. This is useful if you're developing an application that doesn't have users yet: you don't need to track new versions. But if your application is being actively used, beware: you might be surprised that your application starts using a new not-backward-compatible library version. To use the latest version:

libraries:
- name: PIL
  version: latest

For a list of supported libraries, please see Third-party Libraries.

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 Application section below, or check out the forums.

Considerations When Migrating Your Application

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 (HRD). If you are currently using the Master/Slave Datastore, you need to migrate your application 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.

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.