I usually code in JavaScript, so I'm fairly new to Python on AWS. I'm trying to use the matplotlib library to create a pie chart with predefined values like so:
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)
However, when I try to run this code, I get an 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'
I'm not sure if I properly added the dependencies as layers. The way did it was:
- Install all dependencies using
pip install -t
- Put dependencies into "python" folder
- Zip the python folder
- Upload the zip file as a layer and add it to the function
How can I fix this error? Runtime: Python 3.10 matplotib version: 3.3.4