[Python] Unable to import module 'lambda_function': No module named 'PIL' even after importing...

Discussão em 'Python' iniciado por Stack, Julho 11, 2025.

  1. Stack

    Stack Membro Participativo

    I'm getting this error "Unable to import module 'lambda_function': No module named 'PIL'" when I try to run an AWS lambda that uses PIL.

    What my current setup involves: I downloaded the wheel file for pillow 11.3.0 on linux_x86 from pypi.org. I unzipped the file to move the internal PIL/pillow/pillow 11.3 folders into a "python" subfolder, as suggested by AWS documentation. I then re-zipped this file and uploaded it to AWS S3. Then, I used AWS SAM to initialize/update a layer that references this zip file (code below). Finally, I updated the lambda to reference the new layer. When I run the lambda, I get the import error above.

    Pillow11Py39:
    Type: AWS::Serverless::LayerVersion
    Properties:
    LayerName: picoe-prd-pillow-py39-layer
    Description: Pillow 11.3 for Python 3.9
    CompatibleRuntimes: [python3.9]
    ContentUri:
    Bucket: wc2h-picoe-prd-code
    Key: lambda/layers/picoe-prd-pillow-11.3-layer.zip
    RetentionPolicy: Retain


    What I've tried previously: I managed to get a different error, "cannot import name '_imaging' from 'PIL'" when I installed PIL for windows, not linux. I've also tried publishing the layer through AWS CLI instead of SAM, and I've tried keeping the folder divisions as-is (i.e. not including the "python" subfolder) and they've all resulted in the same Import Error. I can't use Klayers as recommended in other posts because my AWS region isn't available in that list, and I don't have access to docker/ec2. Any ideas?

    Continue reading...

Compartilhe esta Página