[Python] cannot import name '_imaging' from 'PIL' in AWS Lambda

Discussão em 'Python' iniciado por Stack, Fevereiro 2, 2025.

  1. Stack

    Stack Membro Participativo

    This has driven me absolutley crazy - I know there are a few similar questions on here with a similar scenario - but I have tried mutliple solutions and nothing works - so I thought I'd request help myself and give full context.

    I am building a very simple process in AWS: upload image into bucket, lambda picks it up, processes it, dumps it in other bucket.

    Everything is set up, terraform, python code etc - however whenever the lambda is invoked, I always get the error:

    [ERROR] Runtime.ImportModuleError: Unable to import module 'lambda_function': cannot import name '_imaging' from 'PIL' (/var/task/PIL/__init__.py)


    Some important things to note:

    • I am dev'ing on Windows (never doing this ever again!!!)
    • I have tried multiple Python runtime versions (Python 3.13, Python 3.12, Python 3.10, Python 3.9
    • Lambda architecture is x86_64

    My python code simply has the following line at the top of the file, which is what is causing me this problem:

    from PIL import Image


    What I have tried:

    • Switching Python runtimes of course
    • Creating a package folder, installing Pillow here, and then zipping up with my Lambda python code
    • Using Lambda layers (which seems like the most popular, easiest and quickest solution) by grabbing my relevant arn arn:aws:lambda:eu-west-2:770693421928:layer:Klayers-p39-pillow:1
    • Using docker to run an amazon linux container (docker run -it --rm -v ${PWD}:/app --workdir /app amazonlinux:2 bash), creating a package folder, installing the Pillow dependencies, and then zipping up with my python code

    No matter what, I always get the error mentioned above.

    Something else to mention is that this was only supposed to be a simple small project which would take an evening or two, so ideally I don't want a huge solution that reinvents the wheel or involves a lot more development (unless that is the only solution at this point).

    If someone is able to replicate my situation, and somehow work out a solution then I would be hugely appretiative.

    As a matter of fact, if someone is able to replicate my situation and also run into the same problem, I'd be very appretaitive as at least I would know I am not going crazy.

    Thanks all.

    Continue reading...

Compartilhe esta Página