Specifying dependencies in Python

There are two ways to specify dependencies for Cloud Functions written in Python: using the pip package manager's requirements.txt file or packaging local dependencies alongside your function.

Specifying dependencies with pip

Dependencies in Python are managed with pip and expressed in a metadata file called requirements.txt. 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.

The requirements.txt file contains one line per package. 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.20.0
numpy

Packaging local dependencies

You can also package and deploy dependencies alongside your function. This approach is useful if your dependency is not available via the pip package manager or if your Cloud Functions environment's internet access is restricted. For example, you might use a directory structure such as the following:

myfunction/
├── main.py
└── localpackage/
    ├── __init__.py
    └── script.py

You can then use code as usual from the included local dependency, localpackage. You can use this approach to bundle any Python packages with your deployment.

Using private dependencies

Dependencies are installed in a Cloud Build environment that does not provide access to SSH keys. Packages hosted in repositories that require SSH-based authentication must be vendored and uploaded alongside your project's code, as described in the previous section.

You can use the pip install command with the -t DIRECTORY flag to copy private dependencies into a local directory before deploying your app, as follows:

  1. Copy your dependency into a local directory:

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

  3. Import from this module to use your dependency:

    import DIRECTORY.DEPENDENCY

Pre-installed packages

The following Python packages are automatically 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:

aiohttp==3.6.2
async-timeout==3.0.1
attrs==19.3.0
cachetools==4.1.1
certifi==2020.6.20
chardet==3.0.4
click==6.7
Flask==1.0.2
google-api-core==1.21.0
google-api-python-client==1.9.3
google-auth==1.18.0
google-auth-httplib2==0.0.3
google-cloud-core==0.28.1
google-cloud-trace==0.19.0
googleapis-common-protos==1.52.0
grpcio==1.30.0
httplib2==0.18.1
idna==2.8
itsdangerous==0.24
Jinja2==2.10
MarkupSafe==1.0
multidict==4.7.6
opencensus==0.1.6
pip==18.0
protobuf==3.12.2
pyasn1==0.4.8
pyasn1-modules==0.2.8
pytz==2020.1
PyYAML==5.3.1
requests==2.21.0
rsa==4.6
setuptools==40.2.0
six==1.15.0
uritemplate==3.0.1
urllib3==1.24.3
Werkzeug==0.14.1
wheel==0.31.1
wrapt==1.10.11
yarl==1.4.2

In addition, the Python runtime includes a number of system packages in the execution environment. If your function uses a dependency that requires a system package that is not listed, you can request a package.