Issue with Importing Pillow Library for Image Processing in AWS Lambda Environment
I attempted to use the Pillow library for image processing tasks, specifically for image resizing and converting images to binary formats. After installing the library and packaging it into a ZIP file for deployment, I encountered the following import error:
Runtime.ImportModuleError: Unable to import module 'handler': cannot import name '_imaging' from 'PIL'
I have tried packaging the Pillow library using multiple environments, including Windows, Linux, and an EC2 instance (AWS Linux), but the issue persists. The error appears to be related to the _imaging module not being properly recognized during import.
- Topics
- ServerlessCompute
- Language
- English
asked 2 years ago2.1K views
- Newest
- Most votes
- Most comments
Hi, Pillow is packaged as a standard Lambda layer.
So,
- Go to https://github.com/keithrozario/Klayers/tree/master/deployments
- Select your Python version
- Open corresponding html file
- Find the ARN of the Pillow library and copy
- Go to the Lambda console
- Open the setup of your Lambda
- Choose Add Layer
- paste the ARN of the Pillow layer that you copied before
Your Lambda should now run with Pillow
Best,
Didier
- Capriole Sports Tech Pvt Ltd2 years ago
@Didier Do I need to do this using Linux, or is it okay to use Windows as well?
The issue you're experiencing with importing the Pillow library in your AWS Lambda environment is a common problem when working with libraries that have C or C++ components, like Pillow. This occurs because the binary dependencies need to be compatible with the Lambda execution environment.
To resolve this issue, you have a few options:
-
Use a Lambda Layer: Create a Lambda Layer containing the Pillow library and its dependencies, compiled specifically for the Lambda environment. This allows you to separate the library from your function code and reuse it across multiple functions.
-
Use a container image: Instead of packaging your function as a ZIP file, you can create a container image that includes the correct version of Pillow and its dependencies. This approach gives you more control over the runtime environment.
-
Build the deployment package in a compatible environment: Use an Amazon Linux environment (like an EC2 instance) that matches the Lambda execution environment to build your deployment package. This ensures that the compiled binaries are compatible.
-
Use the AWS SAM CLI with the --use-container option: If you're using AWS SAM for deployment, you can use the --use-container option when running 'sam build'. This downloads a container base image compatible with the Lambda execution environment and builds your function's deployment package in a Docker container using that image.
-
Manually build the .zip deployment package: Build your function's .zip deployment package yourself using the correct source distribution binary for Pillow. Save the .zip file in the directory you specify as the CodeUri in your AWS SAM template or use it for direct upload to Lambda.
When working with image processing libraries like Pillow in Lambda, it's crucial to ensure that you're using the correct source distribution binary that's compatible with the Lambda execution environment. This often means compiling the library in an environment that closely matches Lambda's, or using pre-built binaries specifically designed for use with Lambda.
Remember to always test your function thoroughly after making these changes to ensure that the Pillow library is working correctly in the Lambda environment.
Sources
Create a serverless file-processing app - AWS Lambda
Troubleshoot deployment issues in Lambda - AWS Lambda
answered 2 years ago
Relevant content
- Accepted Answer
asked 3 years ago
- Accepted Answer
asked 6 years ago
