admin管理员组

文章数量:1310012

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.

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.

Share Improve this question edited Feb 2 at 9:58 John Rotenstein 270k28 gold badges446 silver badges530 bronze badges Recognized by AWS Collective asked Feb 2 at 0:57 user8981199user8981199 276 bronze badges 3
  • 1 How are you installing pillow? _imaging is a DLL that has to match the operating system and CPU where it is run. – Tim Roberts Commented Feb 2 at 1:13
  • I've been running pip3 install --target /tmp/layer/python "pillow==9.5.0" when installing into the packages folder, or when installing into the packages folder but when using docker on that amazon linux container @TimRoberts – user8981199 Commented Feb 2 at 1:15
  • I admit I'm not up on AWS lambdas. Is that pip3 install command being run on the lambda system? My theory, based on limited understanding, is that you are somehow selecting the Windows version of pillow and copying THOSE DLLs to a LInux server. That's doomed to fail. If that's not what is happening, then ignore this comment. – Tim Roberts Commented Feb 2 at 5:29
Add a comment  | 

2 Answers 2

Reset to default 0

I got around this by using Docker. The lambda function is expecting the package to be built with Amazon's specific python. This can be done by using their docker image. I have a lambda layer that uses PIL as a dependency, with some other packages in my requirements.txt file.

This is how I've set up these environment variables in order to package the ZIP:

PROJECT_DIR: Holds the absolute path to your project directory on the host machine, which is mounted into the container.

LAMBDA_LAYER_DIR: Specifies the base directory for your AWS Lambda layer resources, with /python appended to set the container’s working directory.

PYTHON_TARGET: Defines the target directory where pip installs all required Python packages for the Lambda layer.

REQUIREMENTS_PATH: Points to the requirements file that lists the Python dependencies to be installed into the target directory. This is the requirements.txt file I mentioned above.

docker run \
  -v "$PROJECT_DIR:$PROJECT_DIR" \
  --entrypoint bash \
  -w "$LAMBDA_LAYER_DIR/python" \
  public.ecr.aws/lambda/python:3.13 \
  -c "dnf install -y gcc python3-devel libjpeg-devel zlib-devel && \
      pip install --upgrade pip && \
      pip install --target $PYTHON_TARGET -r $REQUIREMENTS_PATH && \
      pip install --target $PYTHON_TARGET . && \
      chmod -R a+w $PYTHON_TARGET"

I was facing the same issue and here is how I fixed it:

  • create new function in Lambda (python 3.9 - x86_64)
  • create new file lambda_function.py in your local device
  • open terminal, then run command cd your/project/path
  • run command pip install --platform manylinux2014_x86_64 --target=package --implementation cp --python-version 3.9 --only-binary=:all: --upgrade pillow

if the process is finished successfully, there will be a new folder named package

  • open it and copy all folders inside
  • paste them in the same folder with lambda_function.py
  • it will look like this:
  • select all, then archive them
  • import the zip file to your lambda function

in my case, those steps can fix the [ERROR] Runtime.ImportModuleError: Unable to import module 'lambda_function': cannot import name '_imaging' from 'PIL' (/var/task/PIL/__init__.py)

I hope that it works for you as well.

本文标签: pythoncannot import name 39imaging39 from 39PIL39 in AWS LambdaStack Overflow