Getting PIL/Pillow 4.2.1 to upload properly to AWS Lambda Py3.6

后端 未结 3 691
温柔的废话
温柔的废话 2021-02-19 04:03

Background

I have been struggling for the past few days to deploy a Lambda that uses Pillow, and I am deploying using Python 3.6. It may be noteworthy a

3条回答
  •  孤街浪徒
    2021-02-19 04:22

    Basically, you have to compile the libraries (eg, PIL) either using Docker or, even better, an EC2 instance.

    1. Launch an Docker container like this: docker run --rm -it -v "%cd%:/code" lambci/lambda:build-python3.6 sh

    2. Inside there, navigate to the /code dir (cd /code), create a virtualenv (virtualenv env), activate it (source env/bin/activate) and finally, install your library (pip install pillow).

    3. Once you have installed your library, you can exit the container. The secret here is to move your package library to the root folder (where your main .py file is). For example, move the folder env/lib/python3.6/site-packages/PIL to the root.

    Then, zip your PIL folder together with your .py file and you're set!

    I just did it and documented it in full here: https://learn-serverless.org/post/deploying-pillow-aws-lambda/

提交回复
热议问题