The Python Runtime

The Cloud Functions Python runtime is based on Python version 3.7.0. For instructions on how to prepare your local machine for Python development, see Setting Up a Python Development Environment.

Selecting the runtime

You can select the Python runtime for your function during deployment.

gcloud

If you are using the gcloud command-line tool, you can specify the runtime by using the --runtime parameter. For example:

gcloud functions deploy NAME --runtime python37 --trigger-http

For more arguments that you can specify when you are deploying, see Deploy using the gcloud tool.

Console

If you are using the GCP Console, you can select the runtime when you create and deploy a function.

  1. In the GCP Console, go to the Cloud Functions Overview page.

    Go to the Cloud Functions Overview page

    Make sure that the project for which you enabled Cloud Functions is selected.

  2. Click Create Function.

  3. Under Runtime, select Python 3.7.

Execution environment

The execution environment includes the runtime, the operating system, packages, and a library that invokes your function.

Base image

For the Python runtime, Cloud Functions uses an execution environment based on Ubuntu 18.04. Updates to the language runtime are done automatically (unless otherwise notified), and include changes in the definition of the base image.

HTTP library

The Python runtime uses Flask to handle incoming requests. This helper invokes your function in response to an incoming request and takes care of other details, such as handling HTTP.

Updates to the execution environment

This runtime receives automatic updates to the version of Python. For example, Cloud Functions currently uses Python version 3.7.0. When a new version of Python is released to the language community, the runtime version might be updated.

Similarly, Cloud Functions might apply updates to other aspects of the execution environment, such as the operating system, packages, and other elements of the environment. These updates help keep your function secure.

Cloud Functions file naming

Your function's entrypoint must be contained in a Python source file named main.py.

Specifying dependencies

A function is allowed to use other third-party libraries as well as other local data. Dependencies in Python are managed with pip and expressed in a metadata file called requirements.txt shipped alongside your function. This file must be in the same directory as the main.py file that contains your function code.

When you deploy your function, Cloud Functions downloads and installs dependencies declared in the requirements.txt file using pip. You can also pre-package fully materialized dependencies alongside your function.

The requirements.txt file contains one line per library. Each line contains the package name, and optionally, the requested version. For more details, see the requirements.txt reference.

The following is an example requirements.txt file:

requests==2.19.0
numpy
Was this page helpful? Let us know how we did:

Send feedback about...

Cloud Functions Documentation