将matplotlib导入AWS Lambda函数的问题

yfwxisqw  于 2023-06-06  发布在  其他
关注(0)|答案(1)|浏览(379)

我通常使用JavaScript编写代码,所以我对AWS上的Python相当陌生。我尝试使用matplotlib库创建一个具有预定义值的饼图,如下所示:

import json
import matplotlib.pyplot as plt
import numpy
import random
import boto3

def lambda_handler(event, context):
    # Generate random data for the pie chart
    labels = ['Label 1', 'Label 2', 'Label 3', 'Label 4', 'Label 5']
    sizes = [random.randint(1, 10) for _ in range(len(labels))]
    colors = ['red', 'blue', 'green', 'orange', 'purple']
    
    # Create the pie chart
    plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%')
    plt.title('Random Pie Chart')
    
    # Save the chart image to a byte buffer
    buffer = BytesIO()
    plt.savefig(buffer, format='png')
    buffer.seek(0)
    
    # Upload the chart image to S3
    s3 = boto3.client('s3')
    bucket_name = 'piechart_bucket'
    file_name = 'pie_chart.png'
    s3.upload_fileobj(buffer, bucket_name, file_name)

但是,当我尝试运行这段代码时,我得到了一个ImportError:

Runtime.ImportModuleError: Unable to import module 'lambda_function': 

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.10 from "/var/lang/bin/python3.10"
  * The NumPy version is: "1.24.3"

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'

我不确定我是否正确地添加了依赖关系层。方法是:
1.使用pip install -t安装所有依赖项
1.将依赖项放入“python”文件夹
1.压缩python文件夹
1.将zip文件作为一个层上传,并将其添加到函数中
如何修复此错误?运行时:Python 3.10 matplotib版本:3.3.4

bqf10yzr

bqf10yzr1#

更新:设法解决问题。
对我来说,问题是我下载依赖项的方式。使用pip install -t .安装依赖项似乎不适用于AWS Lambda,所以我在网上搜索了其他下载依赖项的方法,我发现这个video在使用PyPI下载whl文件时很有用。似乎在下载需要numpy的库时,您应该手动安装它们,而不是使用pip。至于我使用的版本,我发现另一篇文章有类似的用例here,所以我只是使用了他们使用的相同版本。

相关问题