2

I am developing a lambda function with python3.6. It uses pytesseract library to convert image to text. I created layer with below folder structure.

python/lib/python3.6/site-packages/PIL
python/lib/python3.6/site-packages/Pillow-6.2.1.dist-info
python/lib/python3.6/site-packages/pytesseract
python/lib/python3.6/site-packages/pytesseract-0.3.1.dist-info

Compressed 'python' folder (as shown above) and uploaded to layers in AWS.

My lambda function code is as below. My code is in python3.6

from PIL import Image
import pytesseract
import json

def lambda_handler(event, context):
    print(pytesseract.image_to_string(Image.open('image002.jpg')))
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

But when I run code I get below error

Unable to import module 'lambda_function': cannot import name '_imaging'

This error is because it is unable to find pillow library (PIL). But it is already included in site-packages folder. I believe there would be some mistake folder structure of layers.

I already tried with folder strutures as guided in below links but no success:

https://stackoverflow.com/a/55711008/1030951

https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html

HarshIT
  • 4,583
  • 2
  • 30
  • 60

2 Answers2

1

After long research I found that the PIL library uses C code internally and that is not directly supported in lambda function for python environment so I was unable to import PIL in that function. Proper solution is to go with ECS and deploy my own Docker image to perform such operation.

HarshIT
  • 4,583
  • 2
  • 30
  • 60
-2

download the library PIL using --target option in pip install and place that lib file in lambda zip file and then upload it.

pip install --target drive/location PIL

This solution is without using layers.

  • I recently tried with that structure as well, Still the same error. see my folder structure screenshot in this link: https://pasteboard.co/IMHDKnA.png – HarshIT Dec 24 '19 at 07:58
  • Then this error is not because of the PIL package. It says - `unable to import module lambda_function` . There might be error in lambda configuration. – Sudarshan Rampuria Dec 24 '19 at 08:44
  • well it is ' unable to import module lambda_function' because it is "unable to import name _imaging''. The entire error message is like this – HarshIT Dec 24 '19 at 08:49