The Python runtime Stay organized with collections Save and categorize content based on your preferences.
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 the
runtime_configandoperating_systemsettings in yourapp.yamlfile tospecify an operating system.Install
gcloudCLI version420.0.0 or later. You can update your CLItooling by running thegcloud components updatecommand. To view your installed version, run thegcloud versioncommand.Optionally, you can specify a runtime version by including the
runtime_versionsetting in yourapp.yamlfile. By default, the latest Python version is used iftheruntime_versionsetting 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.7For 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:flexSee theapp.yamlreference page for more information.
The interpreters that are deployed for each version setting are shown in thefollowing table:
python_version setting | Deployed interpreter | Runtime ID | app.yaml example |
|---|---|---|---|
2 (default) | 2.7.12 | python2 | runtime_config:python_version: 2 |
3.4 | 3.4.8 | python34 | runtime_config:python_version: 3.4 |
3.5 | 3.5.9 | python35 | runtime_config:python_version: 3.5 |
3 or3.6 | 3.6.10 | python36 | runtime_config:python_version: 3 |
3.7 | 3.7.9 | python37 | runtime_config:python_version: 3.7 |
3 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.0An example entrypoint using gunicorn for a Flask application:
entrypoint:gunicorn-b:$PORTmain:appAn example entrypoint using gunicorn for a Django application:
entrypoint:gunicorn-b:$PORTmydjangoapp:wsgiexec 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--callableappFor applications that can handle requests without a WSGI server, you can just execute a Python script:
entrypoint:pythonmain.pyRecommended 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:appYou 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+1Additionally, 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 Variable | Description |
|---|---|
GAE_INSTANCE | The name of the current instance. |
GAE_MEMORY_MB | The amount of memory available to the application process. |
GAE_SERVICE | The service name specified in your application'sapp.yaml file, or if no service name is specified, it is set todefault. |
GAE_VERSION | The version label of the current application. |
GOOGLE_CLOUD_PROJECT | The Project ID associated with your application, which is visible in the Google Cloud console |
PORT | The 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.