Python 3.9 is now generally available.

Accessing App Engine bundled services for Python 3

This page describes how to install and use App Engine bundled services with the Python 3 runtime for the standard environment. Your app can access the bundled services through the App Engine services SDK for Python 3.

Using App Engine bundled services in your runtime migration projects

Historically, App Engine standard environment provided several scalable, proprietary services for app development. These services were bundled with first-generation runtimes (also called App Engine legacy runtimes), which include Python 2, Java 8, Go 1.11, and PHP 5.5. Because several of the legacy runtimes are no longer maintained by their respective open-source communities, as an App Engine developer, you may face hard choices on whether to invest time and effort in migrating to a newer runtime or else incur growing costs to retain your app on a legacy runtime.

Recognizing this challenge, Google Cloud is committed to providing you with a more incremental migration path to newer runtimes. To reduce runtime migration complexity, Google Cloud now supports a set of App Engine bundled services and their associated APIs on second-generation runtimes, which include Python 3, Java 11, and Go 1.12 or higher. Your app can call bundled services APIs for second-generation runtimes through language-idiomatic libraries.

Accessing App Engine bundled services in first- and second generation runtimes
Figure 1. How apps access the App Engine bundled services

In runtime migration scenarios, you often also have the option to use Google Cloud products that offer similar functionality as the App Engine bundled services. For the bundled services that are not available as separate products in Google Cloud, such as image processing, search, and messaging, you can use third-party providers or other workarounds as suggested in the Runtimes guide.

Before you begin

Installing the App Engine services SDK

To install the App Engine services SDK, follow these steps:

  1. Include the SDK with your app by adding the following line to your requirements.txt file:

    appengine-python-standard>=0.2.2
    

    You can find the SDK on GitHub under the appengine-python-standard repo, and on PyPI as an alpha package.

  2. Add the following code in your main Python script. This code creates WSGI middleware that sets the variables required to enable your API calls.

    Flask

    from flask import Flask
    from google.appengine.api import wrap_wsgi_app
    
    app = Flask(__name__)
    app.wsgi_app = wrap_wsgi_app(app.wsgi_app)
    

    Django

    from DJANGO_PROJECT_NAME.wsgi import application
    from google.appengine.api import wrap_wsgi_app
    
    app = wrap_wsgi_app(application)
    

    Pyramid

    from pyramid.config import Configurator
    from google.appengine.api import wrap_wsgi_app
    
    config = Configurator()
    # make configuration settings
    app = config.make_wsgi_app()
    app = wrap_wsgi_app(app)
    
  3. Add the following line to your app.yaml before deploying your app:

    app_engine_apis: true
    
  4. To deploy your app, use the gcloud beta app deploy command.

Migration considerations

You should be aware of the following considerations if you are migrating to the Python 3 runtime and your app uses App Engine bundled services:

Testing and deployment

  • To test the App Engine bundled services functionality in your Python 3 app, you can use the local development server. When running the dev_appserver.py command, you must set the --runtime_python_path argument to include a path to the Python 3 interpreter. For example:

    dev_appserver.py --runtime_python_path=/usr/bin/python3
    

    You can also set the argument to a comma-separated key-value list like this:

    dev_appserver.py --runtime_python_path="python27=/user/bin/python2.7,python3=/usr/bin/python3"
    
  • To deploy your app, use the gcloud beta app deploy command.

Web frameworks

  • webapp2 is not bundled or supported in Python 3, so any application needs to be rewritten to make use of any WSGI-compatible framework (such as Flask).

    A recommended migration strategy is to first replace the use of webapp2 in your Python 2.7 app with Flask (or an alternative web framework such as Django, Pyramid, Bottle, or web.py), while remaining on Python 2.7. Then, when your updated app is stable, migrate the code to Python 3 and deploy and test using App Engine for Python 3.

    For examples of how to convert Python 2.7 apps which use webapp2 to use the Flask framework, you can refer to these additional resources.

Using handlers

  • A Python 3 app can only have one script associated with it, so if your app.yaml has multiple script handlers mapping URLs to different scripts, you will need to combine those scripts into one which handles the URL routing.

    The following example shows the handler differences in the app.yaml file for the respective runtimes.

    Python 2

    handlers:
    - url: /static/*
      static_dir: static
    - url: /public
      script: public.app
    - url: /members
      script: members.app
      login: required
    

    Python 3

    handlers:
    - url: /static/*
      static_dir: auto
    - url: /members
      script: auto
      login: required
    

    Your Python 3 app must handle URL routing (for example, with Flask decorators).

    If you want to use multiple script handlers with different URL patterns, or if you want to use other attributes in your handlers, each handler must specify script: auto.

    You can also override the default startup behavior by specifying an entrypoint field in your app.yaml file.

Thread safety

  • Apps are assumed to be thread safe. API calls must be made on the request thread.

Using URL Fetch

  • If your Python 3 app uses the URL Fetch API, the X-Appengine-Inbound-Appid request header is added when your app sends a request to another App Engine app. This allows the receiving app to verify the calling app's identity. To learn more, see Migrating outbound requests.