代码之家  ›  专栏  ›  技术社区  ›  Ewan

Lambda层Pandas(numpy)导致依赖性错误

  •  1
  • Ewan  · 技术社区  · 2 年前

    我有以下Lambda函数:

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

    我有熊猫(和依赖项)作为一个层包括在内。我可以在文件中看到numpy和其他依赖项(以及其他文件和文件夹):

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

    当我运行该函数时,我会得到以下错误:

    {
      "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": []
    }
    

    知道我为什么会犯这个错误吗?

    1 回复  |  直到 2 年前
        1
  •  2
  •   user21821035    2 年前

    之所以经常发生此错误,是因为部署包中的Pandas版本与Lambda执行环境不兼容。Pandas包含本地二进制文件,因此您需要包含与AmazonLinux兼容的版本以及您为功能配置的体系结构。

    Lambda开发指南中的这一部分很好地说明了如何在使用Python部署函数时处理本机二进制文件: https://docs.aws.amazon.com/lambda/latest/dg/python-package.html#python-package-native-libraries