简体   繁体   中英

How can I create one local library to use with AWS Lambda (handling with “cannot import name ' _imaging'” error from PIL library)?

I am trying to use the Pillow Image in my project, passing it as a local library to my AWS Lambda Function use.

The files are located like this:

Backend(folder with the lambda_handler code)

Libraries(folder with some folders to store some libraries)

PIL(folder with the files inside Libraries)

My lambda_handler code is importing Image like this:

from Libraries.PIL import Image

The only time that I use Image is to open one Image as an argument to one function.

When I upload and test the Lambda Function, I am receiving the following error:

{
    "errorMessage": "Unable to import module 'Backend/lambda_function': cannot import name '_imaging' from 'Libraries.PIL' (/var/task/Libraries/PIL/__init__.py)",
    "errorType": "Runtime.ImportModuleError"
}

I already uninstall all Pillow versions and tried using only one version, the most recent version (7.2.0), and some old versions (6.0.0 and 5.0.0).

I am using Python 3.7

Note: Running the program locally everything works ok, but when I run it in AWS Lambda the I receive the error.

The easiest way to use Pillow, which also could solve your issue, is through Lambda layers .

A popular repository with such layers (including pillow) is keithrozario / Klayers on github. To use it, you would locate ARN of the layer based on your region . The list of the ARNs for python 3.7 is here .

For example, for us-east-1 the layer added would be:

在此处输入图像描述

And in your function you would use it as follow eg:

from PIL import Image

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM