Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot import name '_imaging' while trying to use Pillow inside AWS Lambda

I am trying to make AWS Lambda function, which uses PIL. Hence I installed PIL inside my project directory with

pip install Pillow -t .

First time it ran on local machine, it caused

ImportError: cannot import name '_imaging'

I though I did something wrong and deleted PIP and Pillow* directories from project directory and reinstalled PIL. Then it worked.

Unfortunately, when I packed everythong into ZIP and posted to AWS, the function started to fail with it again

Traceback (most recent call last):
  File "/var/task/myfile.py", line 9, in lambda_handler
    from PIL import Image
  File "/var/task/PIL/Image.py", line 64, in <module>
    from . import _imaging as core
ImportError: cannot import name '_imaging'

What is the exact reason of this error and how fix it?

Below are file list in my project dir:

$ ls
bin                          chardet-3.0.4.dist-info  Pillow-5.2.0.dist-info     ThumbnailEnergent_Lambda.zip
certifi                      idna                     requests                   myfile.py
certifi-2018.4.16.dist-info  idna-2.7.dist-info       requests-2.19.1.dist-info  urllib3
chardet                      PIL                      tests                      urllib3-1.23.dist-info
like image 433
Dims Avatar asked Nov 02 '25 15:11

Dims


1 Answers

I was actually having this issue today, and found that my Lambda runtime language was python3.6 because I was using a Cloudformation template I had copied from an earlier project. The problem was that I had downloaded Pillow using Python 3.7 for my zip file, and Pillow names its _imaging C module files with the Python runtime.

In order to fix this I just had to change my runtime language from Python 3.6 to Python 3.7. So just make sure the Python version is exactly the same for your upload script and your Lambda function.

like image 119
Patrick Avatar answered Nov 04 '25 06:11

Patrick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!