The Python runtime

Note: SomePython runtimes have reachedend of support. You cannot re-deployversions that use runtimes after their end of support date. We recommend thatyouupgrade your appto use the latest version of Python.

The Python runtime is the software stack responsible forinstalling your application code and dependencies, and then running thatapplication in the flexible environment.

Python versions

Python 3.14 (preview) usesbuildpacks. For the full list of supportedPython versions, and their corresponding Ubuntuversion, see theRuntime support schedule.

To use asupportedPython version, you must:

  • Include theruntime_config andoperating_system settings in yourapp.yaml file tospecify an operating system.

  • Installgcloud CLI version420.0.0 or later. You can update your CLItooling by running thegcloud components updatecommand. To view your installed version, run thegcloud version command.

  • Optionally, you can specify a runtime version by including theruntime_versionsetting in yourapp.yaml file. By default, the latest Python version is used iftheruntime_version setting is not specified.

Examples

  • To specify Python 3.14 (preview) on Ubuntu 24:

    runtime:pythonenv:flexentrypoint:gunicorn -b :$PORT main:appruntime_config:operating_system:"ubuntu24"runtime_version:"3.14"
  • To specify the latest supported Python version on Ubuntu 24:

    runtime:pythonenv:flexentrypoint:gunicorn -b :$PORT main:appruntime_config:operating_system:"ubuntu24"

See theapp.yamlreference page for more information.

Previous runtime versions

Warning: Pythonversion 3.7 and earlier have reached the end of support.App Engine blocks you from deploying your applications using runtimes that havereached end of support. We recommend that you migrate your app to use asupported versionof Python or use acustom runtime.

For Pythonversion 3.7 and earlier, you specify a version using theruntime_config andpython_version settings in your application'sapp.yamlfile.

Example

runtime:pythonenv:flexentrypoint:gunicorn-b:$PORTmain:appruntime_config:python_version:3.7

For Python versions 3.7 and earlier, the default interpreter isPython 2.7.12 ifruntime_config orpython_version are omitted. For example, you can usethe default runtime by specifyingruntime: python in yourapp.yaml file:

runtime:pythonenv:flex

See theapp.yamlreference page for more information.

The interpreters that are deployed for each version setting are shown in thefollowing table:

python_version settingDeployed interpreterRuntime IDapp.yaml example
2 (default)2.7.12python2runtime_config:
python_version: 2
3.43.4.8python34runtime_config:
python_version: 3.4
3.53.5.9python35runtime_config:
python_version: 3.5
3 or3.63.6.10python36runtime_config:
python_version: 3
3.73.7.9python37runtime_config:
python_version: 3.7
Note: As new minor versions of Python 3 are released, there might be a periodwhere using the generic version setting3 points to the previous minor version. Thegcloud CLI release notes will indicate when3 willpoint to the newest minor version number.

Support for other Python runtimes

If you need to use a Python version that isn'tsupported, you can create acustom runtime and select avalid base image with the Python version you need.

For Google-supplied base images orDocker Python base images,seeBuilding custom runtimes.

To further investigate containerizing App Engine apps forCloud Run, see themigration guide.

Dependencies

The runtime looks for arequirements.txtfile in your application's source directory and usespipto install any dependencies before starting your application. For moreinformation on declaring and managing packages, seeUsing Python Libraries.

If your app requires private dependencies, you need to use acustom runtime based on the Pythonruntime to install the appropriate packages.

Application startup

The runtime starts your application using theentrypoint defined in yourapp.yaml file. The entrypointshould start a process that responds to HTTP requests on the port defined by theenvironment variablePORT.

Most web applications use aWSGI server such asGunicorn,uWSGI orWaitress.

Before you can use one of these servers, you must add them as a dependency in your application'srequirements.txt.If you usegunicorn for your Flask application, ensure that your application'sPython version iscompatible withgunicorn.

The runtime ensures that all dependencies are installed before your entrypoint is called.

Flask==2.0.2gunicorn==20.1.0

An example entrypoint using gunicorn for a Flask application:

entrypoint:gunicorn-b:$PORTmain:app

An example entrypoint using gunicorn for a Django application:

entrypoint:gunicorn-b:$PORTmydjangoapp:wsgi
Note: The entrypoint command will be executed bybash'sexec function.Shell variables such as$PORT will be replaced normally, but shell syntax suchasif,for andwhile embedded in theentrypoint command will notfunction.

Gunicorn is the recommended WSGI server, but it's completely possible to use any other WSGI server. For example, here is an entrypoint that uses uWSGI with Flask:

entrypoint:uwsgi--http:$PORT--wsgi-filemain.py--callableapp

For applications that can handle requests without a WSGI server, you can just execute a Python script:

entrypoint:pythonmain.py

Recommended Gunicorn configuration

The basic entrypoint examples shown above are intended to be starting pointsand may work for your web applications. Most applications, however, will need tofurther configure the WSGI server. Instead of specifying all of the settings onthe entrypoint, create agunicorn.conf.py file in your project root directory,where yourapp.yaml file is located, and specify it in your entrypoint:

entrypoint:gunicorn-cgunicorn.conf.py-b:$PORTmain:app

You can read about all of Gunicorn's configuration values in itsdocumentation.

Workers

Gunicorn uses workers to handle requests. By default, Gunicorn usessync workers. This worker class is compatible with all web applications, but each worker can only handle one request at a time. By default, gunicorn only uses one of these workers. This can often cause your instances to be underutilized and increase latency in applications under high load.

We recommend setting the number of workers to 2-4 times the number of CPU cores for your instance plus one. You can specify this ingunicorn.conf.py as:

importmultiprocessingworkers=multiprocessing.cpu_count()*2+1

Additionally, some web applications that are mostly I/O bound can see aperformance improvement by using adifferent worker class.If your worker class requires additional dependencies such as gevent or tornado, those dependencies will need to be declared in your application'srequirements.txt.

HTTPS and forwarding proxies

App Engine terminates the HTTPS connection at the load balancer and forwards therequest to your application. Most applications do not need to know if therequest was sent over HTTPS or not, but applications that do need thisinformation should configure Gunicorn to trust the App Engine proxy in theirgunicorn.conf.py:

forwarded_allow_ips='*'secure_scheme_headers={'X-FORWARDED-PROTO':'https'}

Gunicorn will now ensure that thewsgi.url_scheme to'https', which most webframeworks will use as indication of the request is secure. If your WSGI serveror framework doesn't support this, just check the value of theX-Forwarded-Proto header manually.

Some applications also need to ascertain the user's IP address. This isavailable in theX-Forwarded-For header.

Note that thesecure_scheme_headers setting ingunicorn.conf.py should beupper case, likeX-FORWARDED-PROTO, but the headers that your code can readwill be in mixed case, likeX-Forwarded-Proto.

Extending the runtime

The flexible environment Python runtime can be used to create a custom runtime.SeeCustomizing the Pythonfor more information.

Environment variables

The following environment variables are set by the runtime environment:

Environment VariableDescription
GAE_INSTANCEThe name of the current instance.
GAE_MEMORY_MBThe amount of memory available to the application process.
GAE_SERVICEThe service name specified in your application'sapp.yaml file, or if no service name is specified, it is set todefault.
GAE_VERSIONThe version label of the current application.
GOOGLE_CLOUD_PROJECTThe Project ID associated with your application, which is visible in the Google Cloud console
PORTThe port that will receive HTTP requests.

You can set additional environment variables in theapp.yaml file.

Metadata server

Each instance of your application can use theCompute Engine metadata server to query information about theinstance, including its host name, external IP address, instance ID, custommetadata, and service account information. App Engine does not allow you to setcustom metadata for each instance, but you can setproject-wide custom metadata and read it from yourApp Engine and Compute Engine instances.

This example function uses the metadata server to get the external IP address of the instance:

METADATA_NETWORK_INTERFACE_URL=("http://metadata/computeMetadata/v1/instance/network-interfaces/0/""access-configs/0/external-ip")defget_external_ip():"""Gets the instance's external IP address from the Compute Engine metadata    server.    If the metadata server is unavailable, it assumes that the application is running locally.    Returns:        The instance's external IP address, or the string 'localhost' if the IP address        is not available.    """try:r=requests.get(METADATA_NETWORK_INTERFACE_URL,headers={"Metadata-Flavor":"Google"},timeout=2,)returnr.textexceptrequests.RequestException:logging.info("Metadata server could not be reached, assuming local.")return"localhost"

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-17 UTC.