Specify dependencies in Python  |  Cloud Functions Documentation  |  Google Cloud (2024)

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

Dependency specification using the Pipfile/Pipfile.lock standard isnot supported. Your project should not include these files.

Specifying dependencies with pip

Dependencies in Python are managed with pip and expressed in a metadata filecalledrequirements.txt.This file must be in the same directory as the main.py file that contains yourfunction code.

When you deploy or redeploy your function, Cloud Functionsuses pip to download and install the latest version of yourdependencies as declared in the requirements.txt file.The requirements.txt file contains one line per package. Each line containsthe package name, and optionally, the requested version. For more details, seethe requirements.txtreference.

To prevent your build from being affected by dependency version changes,consider pinning your dependency packages to a specific version.

The following is an example requirements.txt file:

functions-frameworkrequests==2.20.0numpy

The Functions Framework is arequired dependency for all functions. Although Cloud Functionsinstalls it on your behalf when the function is created, we recommendthat you include it as an explicit dependency for clarity.

If yourfunction relies on private dependencies, we recommend that youmirror functions-framework to your private registry. Include the mirroredfunctions-framework as a dependency to your function to avoid installing thepackage from the public internet.

Packaging local dependencies

You can also package and deploy dependencies alongside your function. Thisapproach is useful if your dependency is not available via the pippackage manager or if your Cloud Functions environment's internetaccess is restricted.

For example, you might use a directory structuresuch as the following:

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

You can then import the code as usual from localpackage using the followingimport statement.

# Code in main.pyfrom localpackage import script

Note that this approach will not run any setup.py files. Packages with thosefiles can still be bundled, but may not run correctly on Cloud Functions.

Vendored dependencies

Vendored dependencies are dependencies whose source is included directlyin your source code package and rebuilt alongside your own code.Use the GOOGLE_VENDOR_PIP_DEPENDENCIES build environment variableto create vendored pip dependencies and avoid installing themduring deployment.

Create vendored dependencies

  1. Ensure that python3 is installed onyour development system.

  2. Declare your application dependenciesin a requirements.txt file in the root directory of your development tree.

  3. Declare Functions Framework as a requirement by includingfunctions-framework on a separate line in your requirements.txt file.

  4. Download your function's dependencies to your local directory. The steps todo this depend on whether the dependency is a Python wheel (*.whl) file ora tar file (*.tar.gz).

    1. If the dependency is a Python wheel (*.whl), download it into the rootdirectory of your development tree with this pip command:

      python3 -m pip download -r requirements.txt --only-binary=:all: \ -d DIRECTORY \ --python-version PYTHON_RUNTIME_NAME \ --platform manylinux2014_x86_64 \ --implementation cp

      Replace:

      • DIRECTORY: the name of the local directory todownload to
      • PYTHON_RUNTIME_NAME: the Python version to use forcompatibility checks

      The resulting directory structure should look like this:

      myfunction/├── main.py└── requirements.txt└── DIRECTORY ├── dependency1.whl └── dependency2.whl
    2. If the dependency is a tar file (*.tar.gz):

      1. If the dependency is written in Python, use pip to download it:

        python3 -m pip download -r requirements.txt \ -d DIRECTORY
      2. If a dependency consists of code written in C or C++, you must downloadand compile it separately.

  5. Deploy your function and its vendored dependencies:

    gcloud functions deploy FUNCTION_NAME \ --runtime PYTHON_RUNTIME_NAME \ --set-build-env-vars GOOGLE_VENDOR_PIP_DEPENDENCIES=DIRECTORY

    Replace:

    • FUNCTION_NAME: the name of the Cloud Functionsfunction you're deploying
    • PYTHON_RUNTIME_NAME: the name of the Python runtime torun your deployed function under - for example python311. This must be thesame Python runtime version as you've used in your local developmentenvironment.
    • DIRECTORY:the name of the directory containing yourvendored dependencies

For more details about using buildpacks, seeBuild a function with buildpacks.

Using private dependencies

Private dependencies from Artifact Registry

An Artifact Registry Pythonrepository can host privatedependencies for your Python function. When deploying to Cloud Functions, thebuild process will automatically generate Artifact Registry credentials for theCloud Build service account. You onlyneed to include the Artifact Registry URL in your requirements.txt withoutgenerating additional credentials. For example:

--index-url REPOSITORY_URLsampleappFlask==0.10.1google-cloud-storage

If your build needs multiple repositories, use anArtifact Registry virtual repositoryto safely control the order that pip searches your repositories.

Private dependencies from other repositories

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

You can use the pip install command with the-t DIRECTORY flag to copy private dependencies intoa 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 DIRECTORYdirectory 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 yourfunction during deployment. If you are using any of these packages in yourfunction code, we recommend that you include the following versions in yourrequirements.txt file:

Python 3.7

aiohttp==3.8.1aiosignal==1.2.0async-timeout==4.0.2attrs==21.4.0cachetools==4.2.4certifi==2021.10.8chardet==4.0.0charset-normalizer==2.0.10click==8.0.3Flask==2.0.2frozenlist==1.2.0google-api-core==2.3.2google-api-python-client==2.34.0google-auth==2.3.3google-auth-httplib2==0.1.0google-cloud-core==2.2.1google-cloud-trace==1.5.1googleapis-common-protos==1.54.0grpcio==1.43.0grpcio-status==1.43.0httplib2==0.20.2idna==3.3itsdangerous==2.0.1Jinja2==3.0.3MarkupSafe==2.0.1multidict==5.2.0opencensus==0.8.0opencensus-context==0.1.2packaging==21.3proto-plus==1.19.8protobuf==3.19.1pyasn1==0.4.8pyasn1-modules==0.2.8pyparsing==3.0.6pytz==2021.3PyYAML==6.0requests==2.27.1rsa==4.8setuptools==60.3.1six==1.16.0uritemplate==4.1.1urllib3==1.26.7Werkzeug==2.0.2wrapt==1.13.3yarl==1.7.2

Python 3.8 and later

click==8.1.7cloudevents==1.9.0deprecation==2.1.0Flask==2.2.5functions-framework==3.0.0gunicorn==20.1.0importlib-metadata==6.7.0itsdangerous==2.1.2Jinja2==3.1.2MarkupSafe==2.1.3packaging==23.1typing_extensions==4.7.1watchdog==1.0.2Werkzeug==2.2.3zipp==3.15.0
* `pip` (latest version)* `setuptools` (latest version)* `wheel` (determined by product requirements)

In addition, the Python runtime includes a number of systempackages in the executionenvironment. If your function uses a dependency that requires a system packagethat is not listed, you can request apackage.

Specify dependencies in Python  |  Cloud Functions Documentation  |  Google Cloud (2024)
Top Articles
Latest Posts
Article information

Author: Aron Pacocha

Last Updated:

Views: 5986

Rating: 4.8 / 5 (48 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Aron Pacocha

Birthday: 1999-08-12

Address: 3808 Moen Corner, Gorczanyport, FL 67364-2074

Phone: +393457723392

Job: Retail Consultant

Hobby: Jewelry making, Cooking, Gaming, Reading, Juggling, Cabaret, Origami

Introduction: My name is Aron Pacocha, I am a happy, tasty, innocent, proud, talented, courageous, magnificent person who loves writing and wants to share my knowledge and understanding with you.