Handling dependencies

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.

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

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 or redeploy your function, Cloud Functions uses pip to download and install the latest version of your dependencies as declared in the requirements.txt file. 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.

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:


The Functions Framework is a required dependency for all functions. Although Cloud Functions installs it on your behalf when the function is created, we recommend that you include it as an explicit dependency for clarity.

If your function relies on private dependencies, we recommend that you mirror functions-framework to your private registry. Include the mirrored functions-framework as a dependency to your function to avoid installing the package from the public internet.

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:

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

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

# Code in main.py
from localpackage import script

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