The Python Runtime

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

To get started with Python on Cloud Functions, see the Quickstart.

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.

Python packages

The following Python packages are installed alongside your function during deployment. If you are using any of these packages in your function code, we recommend that you include the following versions in your requirements.txt file:

click==6.7
Flask==1.0.2
itsdangerous==0.24
Jinja2==2.10
MarkupSafe==1.0
pip==18.0
requests==2.19.1
setuptools==40.2.0
Werkzeug==0.14.1
wheel==0.31.1

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.1. 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.

Source code structure

Your function's entrypoint must be defined 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. See Private dependencies for details.

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

Private dependencies

Dependencies are installed in a Cloud Build environment that does not provide access to SSH keys. Packages hosted on repositories that require SSH-based authentication must be vendored and uploaded alongside your project's code. Use pip with the -t DIRECTORY flag to copy dependencies into a local folder before deploying your app, as follows:

  1. Copy dependencies into a local folder:

    pip install -t lib dependency
    
  2. Add an empty __init__.py file to the lib directory to turn it into a module.

  3. Import from this module:

    import lib.dependency
    
Was this page helpful? Let us know how we did:

Send feedback about...

Cloud Functions Documentation