可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I've been trying to run my code in AWS Lambda which imports pandas. So here is what I've done. I have a python file which contains a simple code as follows(This file has the lambda handler)
import json print('Loading function') import pandas as pd def lambda_handler(event, context): return "Welcome to Pandas usage in AWS Lambda"
- I have zipped this python file along with numpy, pandas and pytz libraries as a deployment package (Did all these in Amazon EC2 linux machine)
- Then uploaded the package into S3
- Created a lambda function(runtime=python3.6) and uploaded the deployment package from S3
But when I test the lambda function in AWS Lambda, I get the below error:
Unable to import module 'lambda_function': Missing required dependencies ['numpy']
I already have numpy in the zipped package but still I get this error. I tried to follow the hints given at Pandas & AWS Lambda but no luck.
Did anyone ran into the same issue. Would appreciate any hint or suggestions to solve this problem.
Thanks
回答1:
EDIT: I figured out finally how to run pandas & numpy in a AWS Lambda python 3.6 runtime environment.
I have uploaded my deployment package to the following repo:
git clone https://github.com/pbegle/aws-lambda-py3.6-pandas-numpy.git
Simply add your lambda_function.py
to the zip file by running:
zip -ur lambda.zip lambda_function.py
Upload to S3 and source to lambda.
ORIGINAL:
The only way I have gotten Pandas to work in a lambda function is by compiling the pandas (and numpy) libraries in an AWS Linux EC2 instance following the steps from this blog post and then using the python 2.7 runtime for my lambda function.
回答2:
To get additional libraries in Lambda we need to compile them on Amazon Linux (this is important if the underlying library is based on C or C++ like for Numpy) and package them in a ZIP file together with the python script you want to run in Lambda.
To get the Amazon Linux compiled version of the libraries. You can either find a version that someone already compiled, like the one by @pbegle, or compile it yourself. To compile it ourself there are two options: - compile the libraries on an EC2 instance https://streetdatascience.com/2016/11/24/using-numpy-and-pandas-on-aws-lambda/ - compile the libraries on a docker version of Lambda environment https://serverlesscode.com/post/scikitlearn-with-amazon-linux-container/
Following the last option with Docker, it is possible to make it work using the instructions in the blog post above and by adding:
pip install --use-wheel pandas
in the script to compile the libraries:
https://github.com/ryansb/sklearn-build-lambda/blob/master/build.sh#L21
回答3:
Slightly duplicate of Cannot find MySQL in NodeJS using AWS Lambda
You need to package your libraries with Lambda. As lambda runs on a public cloud, you cannot configure it.
Now in your case, as you are using pandas, you need to package Pandas with your zip. Get a path to pandas(for example: /Users/dummyUser/anaconda/lib/python3.6/site-packages) and copy the library to the place where you have your lambda function code. Inside your code, refer to pandas from your local copy. While uploading, zip the whole set(code + libraries), and upload as you will. It should work.
回答4:
I've been struggling with a similar error while trying to use the python3.6 engine. When I switched to 2.7 it worked fine for me. I used Amazon AMI to create my zip file, but it has only python3.5, not 3.6. I guess the version mismatch was the reason. But it's just a guess, I haven't tried the process on a python3.6 installation yet.