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.
Dependency specification using the Pipfile/Pipfile.lock standard is currently 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 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 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.
Using private dependencies
Private dependencies from Artifact Registry
An Artifact Registry Python
repository can host private
dependencies for your Python function. When deploying to Cloud Functions, the
build process will automatically generate Artifact Registry credentials for the
Cloud Build service account. You only
need to include the Artifact Registry URL in your requirements.txt
without
generating additional credentials. For example:
--extra-index-url REPOSITORY_URL
sampleapp
Flask==0.10.1
google-cloud-storage
Private dependencies from other repositories
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:
Copy your dependency into a local directory:
pip install -t DIRECTORY DEPENDENCY
Add an empty
__init__.py
file to theDIRECTORY
directory to turn it into a module.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:
Python 3.7
Python 3.8 and later
* `pip` (latest version)
* `setuptools` (latest version)
* `wheel` (determined by product requirements)
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.