node package manager
Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »


Serverless Python Requirements

serverless CircleCI appveyor npm

A Serverless v1.x plugin to automatically bundle dependencies from requirements.txt and make them available in your PYTHONPATH.

Requires Serverless >= v1.12


sls plugin install -n serverless-python-requirements

🍎🍺🐍 Mac Brew installed Python notes

Cross compiling!

Compiling non-pure-Python modules or fetching their manylinux wheels is supported on non-linux OSs via the use of Docker and the docker-lambda image. To enable docker usage, add the following to your serverless.yml:

    dockerizePip: true

The dockerizePip option supports a special case in addition to booleans of 'non-linux' which makes it dockerize only on non-linux environments.

To utilize your own Docker container instead of the default, add the following to your serverless.yml:

    dockerImage: <image name>:tag

This must be the full image name and tag to use, including the runtime specific tag if applicable.

Alternatively, you can define your Docker image in your own Dockerfile and add the following to your serverless.yml:

    dockerFile: ./path/to/Dockerfile

With Dockerfile the path to the Dockerfile that must be in the current folder (or a subfolder). Please note the dockerImage and the dockerFile are mutually exclusive.

To install requirements from private git repositories, add the following to your serverless.yml:

    dockerizePip: true
    dockerSsh: true

The dockerSsh option will mount your $HOME/.ssh/id_rsa and $HOME/.ssh/known_hosts as a volume in the docker container. If your SSH key is password protected, you can use ssh-agent because $SSH_AUTH_SOCK is also mounted & the env var set. It is important that the host of your private repositories has already been added in your $HOME/.ssh/known_hosts file, as the install process will fail otherwise due to host authenticity failure.

🏁 Windows notes

Pipenv support ✨🍰✨

If you include a Pipfile and have pipenv installed instead of a requirements.txt this will use pipenv lock --r to generate them. It is fully compatible with all options such as zip and dockerizePip. If you don't want this plugin to generate it for you, set the following option:

    usePipenv: false

Dealing with Lambda's size limitations

To help deal with potentially large dependencies (for example: numpy, scipy and scikit-learn) there is support for compressing the libraries. This does require a minor change to your code to decompress them. To enable this add the following to your serverless.yml:

    zip: true

and add this to your handler module before any code that imports your deps:

  import unzip_requirements
except ImportError:

Omitting Packages

You can omit a package from deployment with the noDeploy option. Note that dependencies of omitted packages must explicitly be omitted too. By default, this will not install the AWS SDKs that are already installed on Lambda. This example makes it instead omit pytest:

      - pytest

Extra Config Options

extra pip arguments

You can specify extra arguments to be passed to pip like this:

      dockerizePip: true
          - --cache-dir
          - .requirements-cache

Customize requirements file name

Some pip workflows involve using requirements files not named requirements.txt. To support these, this plugin has the following option:

    fileName: requirements-prod.txt

Customize Python executable

Sometimes your Python executable isn't available on your $PATH as python2.7 or python3.6 (for example, windows or using pyenv). To support this, this plugin has the following option:

    pythonBin: /opt/python3.6/bin/python

Manual invocations

The .requirements and using zip support) files are left behind to speed things up on subsequent deploys. To clean them up, run sls requirements clean. You can also create them (and unzip_requirements if using zip support) manually with sls requirements install.

Invalidate requirements caches on package

If you are using your own Python library, you have to cleanup .requirements on any update. You can use the following option to cleanup .requirements everytime you package.

    invalidateCaches: true

🍎🍺🐍 Mac Brew installed Python notes

Brew wilfully breaks the --target option with no seeming intention to fix it which causes issues since this uses that option. There are a few easy workarounds for this:


  • Create a virtualenv and activate it while using serverless.


Also, brew seems to cause issues with pipenv, so make sure you install pipenv using pip.

🏁 Windows dockerizePip notes

For usage of dockerizePip on Windows do Step 1 only if running serverless on windows, or do both Step 1 & 2 if running serverless inside WSL.

  1. Enabling shared volume in Windows Docker Taskbar settings
  2. Installing the Docker client on Windows Subsystem for Linux (Ubuntu)


  • @dschep - Lead developer & maintainer
  • @azurelogic - logging & documentation fixes
  • @abetomo - style & linting
  • @angstwad - deploy --function support
  • @mather - the cache invalidation option
  • @rmax - the extra pip args option
  • @bsamuel-ui - Python 3 support
  • @suxor42 - fixing permission issues with Docker on Linux
  • @mbeltran213 - fixing docker linux -u option bug
  • @Tethik - adding usePipenv option
  • @miketheman - fixing bug with includes when using zip option
  • @wattdave - fixing bug when using deploymentBucket
  • @heri16 - fixing Docker support in Windows
  • @ryansb - package individually support
  • @cgrimal - Private SSH Repo access in Docker & dockerFile option to build a custom docker image.