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.
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.
If you are using the GCP Console, you can select the runtime when you create and deploy a function.
In the GCP Console, go to the Cloud Functions Overview page.
Make sure that the project for which you enabled Cloud Functions is selected.
Click Create Function.
Under Runtime, select Python 3.7.
The execution environment includes the runtime, the operating system, packages, and a library that invokes your function.
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.
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.
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
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
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
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.
requirements.txt file contains one line per library. Each line contains
the package name, and optionally, the requested version. For more details, see
The following is an example
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:
Copy dependencies into a local folder:
pip install -t lib dependency
Add an empty
__init__.pyfile to the
libdirectory to turn it into a module.
Import from this module: