在使用AWS Lambda导入pandas时出现了NumPy错误。

9
我目前在将库 pandas 导入 AWS Lambda 函数时遇到问题。我尝试了两种方案:
  • 直接将 pandas 安装到与我的 lambda_function 相同的文件夹中,并上传压缩文件。

  • 创建一个层,并上传以下结构的压缩文件:

- python
    - lib
        - python3.8
            - site-packages
                - all the pandas packages here

我的 lambda_function 只是:

import json
import pandas as pd

def lambda_handler(event, context):
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

这是我的错误:

START RequestId: 9e27641e-587b-4be2-b9be-c9be85007f9e Version: $LATEST
[ERROR] Runtime.ImportModuleError: Unable to import module 'main': Unable to import required dependencies:
numpy: 

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

    https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  * The Python version is: Python3.8 from "/var/lang/bin/python3.8"
  * The NumPy version is: "1.21.1"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: No module named 'numpy.core._multiarray_umath'

还有其他方法吗?我不想在这个任务中使用Docker。谢谢!

3个回答

5
我已经解决了问题,感谢这篇文章:https://korniichuk.medium.com/lambda-with-pandas-fd81aa2ff25e 在我的情况下,我无法通过pip正常安装库,因为我使用的是Windows机器。你必须安装pandas和numpy的Linux版本。由于我使用的是Python 3.8,所以我安装了这些版本:
  • numpy-1.21.1-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl
  • pandas-1.3.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
下载完包之后,我替换了最初通过pip install pandas安装的pandas和numpy文件夹。我使用了我在问题中展示的第一个方案。

1

补充一下,这里是一些非常有用的库集合,作为层使用

AWSome Lambda Layers

感谢贡献者!


0
前往Lambda控制台,点击Lambda底部的“添加层”,选择AWS层,然后点击AWSSDKPandas-Python311(或与您的Python版本相关的层,因为我使用的是Python3.11)。
您将能够运行pandas,以及它加载的任何其他包,如numpy。

Screenshot of how to find AWSSDKPandas-Python3.XX layer to use Pandas


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接