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 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.