Installing numpy on Mac to work on AWS Lambda

2024/9/20 0:12:12

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 multiarraynumpy extension module failed. Most likely you are trying to import afailed build of numpy. If you're working with a numpy git repo, trygit 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 numpydef 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?

Answer

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

https://en.xdnf.cn/q/72216.html

Related Q&A

python- how to get the output of the function used in Timer

I want to run a function for 10s then do other stuff. This is my code using Timerfrom threading import Timer import timedef timeout():b=truereturn ba=false t = Timer(10,timeout) t.start()while(a==f…

Create automated tests for interactive shell based on Pythons cmd module

I am building an interactive shell using Python 3 and the cmd module. I have already written simple unit tests using py.test to test the individual functions, such as the do_* functions. Id like to c…

Matplotlib with multiprocessing freeze computer

I have an issue with matplotlib and multiprocessing. I launch a first process, where I display an image and select an area, and close the figure. Then I launch another process, where I call a graph fun…

Pull Tag Value using BeautifulSoup

Can someone direct me as how to pull the value of a tag using BeautifulSoup? I read the documentation but had a hard time navigating through it. For example, if I had:<span title="Funstuff&qu…

What is the practical difference between xml, json, rss and atom when interfacing with Twitter?

Im new to web services and as an introduction Im playing around with the Twitter API using the Twisted framework in python. Ive read up on the different formats they offer, but its still not clear to m…

how to grab from JSON in selenium python

My page returns JSON http response which contains id: 14Is there a way in selenium python to grab this? I searched the web and could not find any solutions. Now I am wondering maybe its just not poss…

Numpy: Array of `arange`s

Is there a way to take...>>> x = np.array([0, 8, 10, 15, 50]).reshape((-1, 1)); ncols = 5...and turn it into...array([[ 0, 1, 2, 3, 4],[ 8, 9, 10, 11, 12],[10, 11, 12, 13, 14],[15, 16, 17…

Understanding model.summary Keras

Im trying to understand model.summary() in Keras. I have the following Convolutional Neural Network. The values of the first Convolution are: conv2d_4 (Conv2D) (None, 148, 148, 16) 448 …

Determine adjacent regions in numpy array

I am looking for the following. I have a numpy array which is labeled as regions. The numpy array represents a segmented image. A region is a number of adjacent cells with the same value. Each region h…

Python: win32gui.SetForegroundWindow

I have just written simple script to launch an applciation and I am trying to use "SendKeys" module to send keystrokes to this application. There is one "Snapshot" button, but I can…