Why is a python aws lambda with a lambda layer containing scikit-learn and pandas failing with `error importing numpy`?

45 Views Asked by At

I have a python 3.9 lambda function which uses scikit-learn and pandas to run a model.

I am creating a lambda layer (I create the layer in a docker container using the base image public.ecr.aws/lambda/python:3.9) which contains these packages in a python/ directory.

Before zipping I remove some files so as to reduce the size of the layer. This is required so I get under the lambda unzipped limit. To do this I run the following commands:

rm -rf *.dist-info
find . -name "tests" -type d | xargs -I{} rm -rf {}
find . -name "docs" -type d | xargs -I{} rm -rf {}
find . -name "__pycache__" -type d | xargs -I{} rm -rf {}

I zip this and upload it as a lambda layer to AWS. I attach this lambda layer to my lambda but when I try to execute I get the following error:

Unable to import required dependencies:
numpy: Error importing numpy: you should not try to import numpy from
        its source directory; please exit the numpy source tree, and relaunch
        your python interpreter from there.

Can anyone help me understand why this error occurs? The directory structure of the lambda layer is as specified in the AWS docs so I've ruled that out as a problem. Maybe some of the files I am removing are of importance but I didn't think so in this case

0

There are 0 best solutions below