0

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:

  1. Install all dependencies using pip install -t
  2. Put dependencies into "python" folder
  3. Zip the python folder
  4. 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

coolbeans
  • 13
  • 2
  • Here is a link to a duplicate question which I believe answers your question: https://stackoverflow.com/questions/35340921/aws-error-from-python-no-module-named-lambda-function – Trygvi Laksafoss May 30 '23 at 11:22
  • Thanks for the reply, I've checked and I'm using the default handler, my function's name is lambda_function.py, so I don't think its the problem. – coolbeans May 31 '23 at 02:00

1 Answers1

1

Update: Managed to fix the problem.

The problem for me was the way I downloaded the dependencies. Using pip install -t . to install the dependencies didn't seem to work for AWS Lambda, so I searched online to see other ways I can download dependencies and I found this useful video on using PyPI to download whl files. It seems like when downloading libraries requiring numpy, you should manually install them instead of using pip. As for the versions I used, I found another article with a similar use case here, so I just used the same versions that they used.

coolbeans
  • 13
  • 2