AWS Lambda Python PIL cannot import name ‘_imaging’

derek urizar
3 min readSep 20, 2020

--

Some libraries like Pillow are deprecated and they are forked for the community to still upgrade them. The problem is the OS base that runs python in AWS because they do not use the forked libraries. For that, AWS creates layers (not only for that but I will not explain that now) for lambdas and you can create one with your own resources on a specific OS and specific version of your programming language ( in this case python) and put layers into your lambda and so you can run Pillow or other libraries with the same problem.

To create layers you need knowledge of docker and scripts for Linux…

But, why will we create water with sugar if it already exists?

In this repository: https://github.com/keithrozario/Klayers
you have a lot of layers that you can use.

I give you an example of how to add one layer when you have a problem with Pillow.

In this lambda, I need to use PIL because I use AWS Recognition Text and I manage Image objects to draw squares and other things. If you see I have the same error:

I HATE THAT MESSAGE because I lost a lot of time trying to solve it >:(

The next we need to do is erase the libraries because we don’t need that in the base code of the lambda for the reasons I explained at the start of this article.

Your lambda now needs to seem like that:

Now, depending on your python versión or your region in AWS you will search in the repository for the specific ARN of the layer that you will need to use [https://github.com/keithrozario/Klayers/blob/master/deployments/python3.8/arns/us-east-1.csv]. In my case is python 3.8 and us-east-1 region.

In this case, the ARN is :
arn:aws:lambda:us-east-1:770693421928:layer:Klayers-python38Pillow:5

Go to your lambda to this part and click on layers:

You will see this:

Click in add layer-> Specified ARN -> paste the ARN

And that’s all, now your lambda function will run fine.

(deploy and test)

Now it’s solved!

--

--