Specifying Dependencies

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

When you deploy to App Engine, the dependencies specified in the requirements.txt file will be installed automatically with your deployed app. You can use any Linux-compatible Python package, including packages that require native C extensions.

By default, App Engine caches fetched dependencies to reduce build times. To install an uncached version of the dependency, use the command:

gcloud beta app deploy --no-cache

Private dependencies

Dependencies are installed in a Cloud Build environment that does not provide access to SSH keys. Packages hosted on repositories that require SSH-based authentication must be copied into your project directory and uploaded alongside your project's code using the pip package manager.

To use private dependencies:

  1. Run pip install -t lib my_module to copy dependencies into a local folder named lib.

  2. Add an empty __init.py__ file to the lib directory to make it a module.

  3. Import the module in your app. For example:

    import lib.my_module
    

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

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. If you don't have virtualenv, install it system-wide using pip:
    pip install virtualenv
  4. Create an isolated Python environment in a directory external to your project and activate it:
    virtualenv env
    env\Scripts\activate
  5. Navigate to your project directory and install dependencies:
    cd YOUR_PROJECT
    pip install -r requirements.txt

This ensures that when you run your app locally, only the dependencies that are declared in the requirements.txt file are available. The dependencies installed by App Engine during deployment are based on the contents of the requirements.txt file, not the contents of the env/ directory.

Tip: Often you will not know your exact dependencies. You can use the virtualenv and pip tools to install dependencies and use pip freeze to write the name and version of all installed packages to your requirements.txt file.

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.

Was this page helpful? Let us know how we did:

Send feedback about...

App Engine standard environment for Python 3 docs