Deploy a python function with numpy

I am trying to deploy a python function with numpy dependency.
Ran pip install numpy -t . in my project directory. Got the following message:
Installing collected packages: numpy Successfully installed numpy-1.12.1
Project folder has numpy and numpy-1.12.1.dist-info directories. handler.py function is very straightforward.

import json
import numpy

def hello(event, context):
    body = {
        "message": "Go Serverless v1.0! Your function executed successfully!",
        "input": event
    }
    response = {
        "statusCode": 200,
        "body": json.dumps(body)
    }
    return response

While invoking the function I get error messgae:

    {
        "errorMessage": "Unable to import module 'handler'"
    }
--------------------------------------------------------------------
START RequestId: 510e1b7f-42f1-11e7-b438-9dedd68ec099 Version: $LATEST
Unable to import module 'handler': 
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.

What is your local operating system? While I haven’t come across this answer personally, it looks like you might’ve uploaded .pyc files, which are probably not going to work on AWS Linux.

I was using OSX. Ended up using ec2 instance. Deployment went fine on ec2 instance with Amazon Linux.
Do you know why it did not work on OSX?

.pyc files are compiled bytecode, and so only work on the platform they were compiled on. When they’re getting packaged up and pushed to Lambda, they’re no long on the same platform (i.e. OSX) they were compiled on.

1 Like

There is a serverless plugin you can use to get around the problem of compiling on OSX. It uses Docker to pip install everything in your requirements.txt file on Linux. https://www.npmjs.com/package/serverless-python-requirements

Good to know what is happening under the hood. Thanks.

Great. Thanks for sharing this.

Hi,

can u tell me how u used the plugin to bypass the dependencies.

Here is the problem that i am facing -

https://stackoverflow.com/questions/45179452/aws-lambda-building-external-dependency-libraries-in-python

You likely haven’t built the packages on the right architecture. I have a blog post which goes through this in more detail and also covers how to correctly setup your path so that your code can import the libraries.

http://blog.brianz.bz/post/structuring-serverless-applications-with-python/

worked for me ,It wasted 4 hrs of mine just to figure out why lambda is not working.

Thanks a lot