6

I'm simply trying to be able to use PIL in my Python 3.8 Lambda.

I'm trying the following steps:

  1. Based on this repo: https://github.com/hidekuma/lambda-layers-for-python-runtime
cd /mydir 
git clone https://github.com/hidekuma/lambda-layers-for-python-runtime.git 
cd lambda-layers-for-python-runtime 
mkdir dist 
docker-compose up --build
  1. Based on this guidance: https://www.splunk.com/en_us/blog/cloud/sharing-code-dependencies-with-aws-lambda-layers.html
aws lambda publish-layer-version --layer-name ImageStorageDependencies
    --description "A Python 3.8 runtime with PIL and boto3 installed." --license-info "MIT" --zip-file fileb://output.zip --compatible-runtimes python3.7 python3.8 --region us-east-2

I then choose my layer in the Lamda configuration, but when I run this code:

import json
import boto3
import io
from PIL import Image


def lambda_handler(event, context): 
    #etc

...I get the error:

[ERROR] Runtime.ImportModuleError: Unable to import module 'lambda_function': cannot import name '_imaging' from 'PIL' 

Where exactly am I going wrong?!?

Salvatore
  • 10,815
  • 4
  • 31
  • 69
Colin
  • 4,025
  • 21
  • 40
  • Hey, I posted a solution here that does not require Docker. You just create a layer, the trick being that you have the correct version of Python locally, which you can install if needed. https://stackoverflow.com/a/74736780/1375627 – MattC Dec 08 '22 at 22:01

3 Answers3

7

Just to clarify, regarding your comment above, Pillow is just a repackaged, updated version of PIL because the original maintainers of PIL stopped working on it a long time ago. When you pip install Pillow, you still import it as PIL. In this case they are the same thing.

To answer your question, the the Pillow install directions mention:

Pillow >= 2.1.0 no longer supports import _imaging. Please use from PIL.Image import core as _imaging instead.

I'm not sure where your code imports _imaging, so I think you have a few options:

  1. Use an older version of Pillow (pre 2.1.0)
  2. Find where you are importing _imaging and replace it with the updated from PIL.Image import core as _imaging
  3. Update to the current version of Pillow (see my update below)

There is a fourth option which is redirecting the import manually, inspired by this question. I would only do this if you can't do one of the first three. Put this somewhere in your code that gets run before you do the import that is breaking things. You may have to tweak this slightly to make it work:

from PIL.Image import core as _imaging
import sys
sys.modules['PIL._imaging'] = _imaging

A later from PIL import _imaging should now really import the new core.

Update:

Updating Pillow may also solve the problem. In 7.2.0, I can import _imaging in the old way:

>>> import PIL
>>> from PIL import _imaging
>>> print(PIL.__version__)
7.2.0
>>> print(_imaging)
<module 'PIL._imaging' from '...\\lib\\site-packages\\PIL\\_imaging.cp37-win_amd64.pyd'>
Salvatore
  • 10,815
  • 4
  • 31
  • 69
  • I didn't get a chance to confirm this yet, but it's certainly the best lead. I'll report back when I get a chance to test. Thanks for the answer. – Colin Aug 26 '20 at 03:46
1

I had this issue with an AWS Lambda. Pillow would load for one of my Lambdas, but not another. I was able to fix it by making sure the Lambda was running on the Architecture "x86_64", not "arm64" in the AWS Lambda Runtime Settings.

Bktrout47
  • 31
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 05 '22 at 16:01
0

I found this answer on the internet:

ImportError: cannot import name _imaging, It can happen if you have PIL installed, then install Pillow on top of it. Go to /usr/local/lib/python2. 7/dist-packages/ and delete anything with "PIL" in the name (including directories). Then re-install Pillow. When I run from PIL import Image or from PIL import Image etc, I get the error: ImportError: cannot import name 'imaging' from 'PIL' (C:\Users\Pruthvi\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\PIL_init.py) So odd since these same exact commands were working fine yesterday when I was exploring the PIL and pillow modules.

I believe that this issue is related to Pillow library.

Raul Barreto
  • 979
  • 5
  • 9
  • Thanks, but check out this file: https://github.com/hidekuma/lambda-layers-for-python-runtime/blob/master/entrypoint.sh . Pillow is installed in the virtual environment, but PIL never is. – Colin Aug 13 '20 at 23:04