pythonpandasnumpyaws-lambdaaws-lambda-layers

Lambda layer Pandas (numpy) causing dependency error


I have the following Lambda function:

import pandas as pd

def lambda_handler(events, context):
    d = {'col1': [1, 2], 'col2': [3, 4]}
    df = pd.DataFrame(data=d)
    print(df)

I have pandas (and dependencies) included as a layer. I can see numpy and other dependencies are in the files (with other files and folders):

python/lib/python3.11/site-packages/numpy
python/lib/python3.11/site-packages/numpy-1.26.4.dist-info

When I run the function I get the following error:

{
  "errorMessage": "Unable to import module 'lambda_function': Unable to import required dependencies:\nnumpy: Error importing numpy: you should not try to import numpy from\n        its source directory; please exit the numpy source tree, and relaunch\n        your python interpreter from there.",
  "errorType": "Runtime.ImportModuleError",
  "requestId": "ebf17462-e3c2-40c0-9a44-a751882ab7d8",
  "stackTrace": []
}

Any idea why I get this error?


Solution

  • This error often occurs because the version of Pandas in your deployment package is not compatible with the Lambda execution environment. Pandas contains native binaries, so you need to include the version that's compatible with Amazon Linux and the architecture you've configured for your function.

    This section in the Lambda dev guide has good instructions on how to deal with native binaries when deploying functions with Python: https://docs.aws.amazon.com/lambda/latest/dg/python-package.html#python-package-native-libraries