Using Python Libraries

Declaring and managing dependencies

Dependencies for python applications are declared in a standard requirements.txt file. For example:

Flask==0.10.1
python-memcached==1.54

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, it's highly recommended to use virtualenv 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 virtualenv to install dependencies, complete the following steps:

Mac OS / Linux

  1. If you don't have virtualenv, install it system-wide using pip.
    sudo pip install virtualenv
  2. Create an isolated Python environment in a directory external to your project and activate it:
    virtualenv env
    source env/bin/activate
  3. Navigate to your project directory and install dependencies:
    cd YOUR_PROJECT
    pip install  -r requirements.txt

Windows

If you have installed the Cloud SDK, you should already have Python 2.7 installed, typically in C:\python27_x64\ (for 64-bit systems). Use Powershell to run your Python packages.

  1. Locate your installation of Powershell.
  2. Right-click on the shortcut to Powershell and start it as an administrator.
  3. Try running the python command. If it's not found, add your Python folder to your environment's PATH.
    $env:Path += ";C:\python27_x64\"
  4. If you don't have virtualenv, install it system-wide using pip:
    python -m pip install virtualenv
  5. Create an isolated Python environment in a directory external to your project and activate it:
    python -m virtualenv env
    env\Scripts\activate
  6. Navigate to your project directory and install dependencies:
    cd YOUR_PROJECT
    python -m 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. You can use the virtualenv and pip to install dependencies and 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
Was this page helpful? Let us know how we did:

Send feedback about...

App Engine flexible environment for Python docs