Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Installing numpy on Mac to work on AWS Lambda

Is there a way to install numpy on a Mac so that it will work when uploaded to AWS Lambda? I have tried a variety of different ways, including using different pip versions, using easy_install, and following this post, but none of them seem to work. I also tried cloning the git repo and building from there, but I also wasn't able to get that to work (though I'm not sure if I copied the right files up after doing that)

The error I'm getting is:

Unable to import module 'lambda_function': Importing the multiarray numpy extension module failed. Most likely you are trying to import a failed build of numpy. If you're working with a numpy git repo, try git clean -xdf (removes all files not under version control). Otherwise reinstall numpy.

Inspired by this post, I was able to pip install numpy in a Linux environment and get it to work on Lambda.

So my question is: Is it possible to install numpy on a Mac so that it works on AWS Lambda?

Environment: MacBook Pro, MacOS 10.12.2, default python version 2.7.10

I've been testing it with a minor variation on the hello-world-python example on Lambda:

from __future__ import print_function
import numpy

def lambda_handler(event, context):
    #print("Received event: " + json.dumps(event, indent=2))
    print("value1 = " + event['key1'])

(Update) Extending the question: Why do some packages work and others don't?

like image 991
Tchotchke Avatar asked Mar 01 '17 14:03

Tchotchke


People also ask

Is NumPy available in Lambda?

AWS Lambda does not include Pandas/NumPy Python libraries by default.

Can you use pandas in Lambda?

In Pandas, we have the freedom to add different functions whenever needed like lambda function, sort function, etc. We can apply a lambda function to both the columns and rows of the Pandas data frame.


1 Answers

Update: the preferred approach now is to just use the AWS-provided Lambda Layer for NumPy/SciPy, which is super easy to do.

In the console, select your function and then, under the "Design" section click on "Layers". Then click "Add a Layer", and select "AWSLambda-Python37-SciPy1x" under AWS Provided (or whatever the equivalent is for the version of Python you're using).

Then you can seamlessly import numpy, scipy, etc. into your code with no issues.

10/26/2020 - Added example screenshot: enter image description here

enter image description here

like image 193
James Shapiro Avatar answered Sep 21 '22 23:09

James Shapiro