Declaring and managing dependencies
Dependencies for python applications are declared in a standard
requirements.txt
file. For example:
Flask==0.10.1
google-cloud-storage
You can use any Linux-compatible Python package in the App Engine flexible environment, including packages that require native (C) extensions.
The Python runtime will automatically install all dependencies
declared in your requirements.txt
during deployment.
Installing dependencies locally
When developing and testing your application locally, we recommended you use
venv
to isolate your application's dependencies from your system
packages. This also ensures that your dependencies will be the same version on
your local machine and the deployed application.
To use venv
to install dependencies, complete the following
steps:
Mac OS / Linux
- Create an
isolated Python environment
in a directory external to your project and activate it:
python3 -m venv env
source env/bin/activate
- Navigate to your project directory and install dependencies:
cd YOUR_PROJECT
pip install -r requirements.txt
Windows
Use PowerShell to run your Python packages.
- Locate your installation of PowerShell.
- Right-click on the shortcut to PowerShell and start it as an administrator.
- Create an
isolated Python environment
in a directory external to your project and activate it:
python -m venv env
env\Scripts\activate
- Navigate to your project directory and install dependencies:
cd YOUR_PROJECT
pip install -r requirements.txt
Now when you run your application you can be confident that only the
dependencies declared in requirements.txt
are available.
Tip: Often you will not know your exact dependencies. After you install
dependencies, you can use pip freeze
to write the name and version of all installed packages to your
requirements.txt
.
Installing a web framework
You'll need to use a web framework to enable your app to serve web requests. You can use any Python web framework including the following:
To use a particular web framework, just add it to your requirements.txt
:
Flask==0.10.1
Installing a WSGI server
Some web frameworks have built-in WSGI servers; however, few of them are suitable for serving production traffic. Most web applications use a standalone WSGI server such as Gunicorn, uWSGI or Waitress. For more information on installing, configuring, and using a WSGI server see application startup.
Installing the Google Cloud Client library
The Google Cloud Client Library for Python is a client library for accessing Google Cloud Platform services that significantly reduces the boilerplate code you have to write. The library provides high-level API abstractions so they're easier to understand. It embraces idioms of Python, works well with the standard library, and integrates better with your codebase. All this means you spend more time creating code that matters to you.
To install the library locally:
pip install google-cloud
The client library can automatically handle authentication for you locally as well by using the Google Cloud SDK.
gcloud auth login