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:
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:
arn:aws:lambda:eu-west-2:770693421928:layer:Klayers-p39-pillow:1
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 codeNo 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:
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:
arn:aws:lambda:eu-west-2:770693421928:layer:Klayers-p39-pillow:1
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 codeNo 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.
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:
lambda_function.py
in your local devicecd your/project/path
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
lambda_function.py
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.
_imaging
is a DLL that has to match the operating system and CPU where it is run. – Tim Roberts Commented Feb 2 at 1:13pip3 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