9

I am a newbie on the AWS & Python and trying to implement a simple ML recommendation system using AWS Lambda function for self-learning. I am stuck on the packaging the combination of sklearn, numpy and pandas. If combined any two lib means (Pandas and Numpy) or (Numpy and Skype) is working fine and deploy perfectly. Because I am using ML system then i need sklearn (scipy and pandas and numpy) which cannot work and getting this error on aws lambda test. What I have done so far : my deployment package from within a python3.6 virtualenv, rather than directly from the host machine. (have python3.6, virtualenv and awscli already installed/configured, and that your lambda function code is in the ~/lambda_code directory):

  1. cd ~ (We'll build the virtualenv in the home directory)

  2. virtualenv venv --python=python3.6 (Create the virtual environment)

  3. source venv/bin/activate (Activate the virtual environment)

  4. pip install sklearn, pandas, numpy

  5. cp -r ~/venv/lib/python3.6/site-packages/* ~/lambda_code (Copy all installed packages into root level of lambda_code directory. This will include a few unnecessary files, but you can remove those yourself if needed)

  6. cd ~/lambda_code

  7. zip -r9 ~/package.zip . (Zip up the lambda package)

  8. aws lambda update-function-code --function-name my_lambda_function --zip-file fileb://~/package.zip (Upload to AWS)

after that getting this error:

**"errorMessage": "Unable to import module 'index'"**

and

START RequestId: 0e9be841-2816-11e8-a8ab-636c0eb502bf Version: $LATEST
Unable to import module 'index': **Missing required dependencies ['numpy']**

END RequestId: 0e9be841-2816-11e8-a8ab-636c0eb502bf
REPORT RequestId: 0e9be841-2816-11e8-a8ab-636c0eb502bf  Duration: 0.90 ms   Billed Duration: 100 ms     Memory Size: 128 MB Max Memory Used: 33 MB  

I have tried this on EC2 instance as well but did not a success.I did the google and read multiple blogs and solution but not worked. Please help me out on this.

2
  • Did you check this and this Commented Mar 26, 2018 at 9:13
  • yes, already tried, but package needed SKLEARN,Numpy and Pandas with no conflict with version. Commented Mar 28, 2018 at 5:34

5 Answers 5

1

u are using python 3.6 . so pip3 install numpy should be used, make a try .

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I have tried with both python 3.6 with pip3, and python 2.7. But it's not working and getting the error.
0

You need to make sure all the dependent libraries AND the Python file containing your function are all in one zip file in order for it to detect the correct dependencies.

So essentially, you will need to have Numpy, Panda and your own files all in one zip file before you upload it. Also make sure that your code is referring to the local files (in the same unzipped directory) as dependencies. If you have done that already, the issue is probably how your included libraries gets referenced. Make sure you are able to use the included libraries as a dependency by getting the correct relative path on AWS once it's deployed to Lambda.

2 Comments

But this is isolated to Numpy or other similarly referenced libraries as well? If Numpy and your other libraries and resources are in the same location but others can be referenced, then that's very strange.
Yes, this is very weird behaviour, I think there is a compatibility issue of Numpy, pandas and sklearn lib. because both pandas and sklearn lib use numpy.
0

So like Wai kin chung said, you need to use pip3 to install the libraries.

so to figure out which python version is default you can type:

which python

or

python -v

So in order to install with python3 you need to type:

python3 -m pip install sklearn, pandas, numpy --user

Once that is done, you can make sure that the packages are installed with:

python3 -m pip freeze

This will show all the python libraries installed with your python model. Once you have the libraries you would want to continue with you regular steps. Of course you would first want to delete everything that you have placed in ~/venv/lib/python3.6/site-packages/*.

cd ~/lambda_code
zip -r9 ~/package.zip

Comments

0

If you're running this on Windows (like I was), you'll run into an issue with the libraries being compiled on an incompatible OS.

You can use an Amazon Linux EC2 instance, or a Cloud9 development instance to build your virtualenv as detailed above.

Or, you could just download the pre-compiled wheel files as discussed on this post: https://aws.amazon.com/premiumsupport/knowledge-center/lambda-python-package-compatible/

Essentially, you need to go to the project page on https://pypi.org and download the files named like the following:

  • For Python 2.7: module-name-version-cp27-cp27mu-manylinux1_x86_64.whl
  • For Python 3.6: module-name-version-cp36-cp36m-manylinux1_x86_64.whl

Then unzip the .whl files to your project directory and re-zip the contents together with your lambda code.

Comments

0

Was having a similar problem on Ubuntu 18.04. Solved the issue by using python3.7 and pip3.7.

Its important to use pip3.7 when installing the packages, like pip3.7 install numpy or pip3.7 install numpy --user

To install python3.7 and pip3.7 on Ubuntu you can use deadsnakes/ppa

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.7

curl https://bootstrap.pypa.io/get-pip.py -o /tmp/get-pip.py
python3.7 /tmp/get-pip.py

This solution should also work on Ubuntu 16.04.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.