Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

python - aws lambda Unable to import module 'lambda_function': No module named 'requests'

I have recently started to use AWS Lambda to use triggers against some python code I have written. I currently have 2 lambda functions, both of which have been created with ZIP files. The second one I created is supposed to test the trigger events.

This is for testing purposes so I'm using the best code of all:

def lambda_handler(event, context):
    print ("Hello World")

However, I get this error back:

Response:
{
  "errorMessage": "Unable to import module 'lambda_function'"
}

Request ID:
"65024f16-172c-11e8-ab26-27ff3322e597"

Function Logs:
START RequestId: 65024f16-172c-11e8-ab26-27ff3322e597 Version: $LATEST
Unable to import module 'lambda_function': No module named 'requests'

END RequestId: 65024f16-172c-11e8-ab26-27ff3322e597
REPORT RequestId: 65024f16-172c-11e8-ab26-27ff3322e597  Duration: 15.93 ms  
Billed Duration: 100 ms     Memory Size: 128 MB Max Memory Used: 22 MB  

Everywhere I have searched for this, the answer was solved by making sure the names for the functions were correct or making sure the .zip file was readable. I have satisfied both of these conditions (the name of the file is lambda_function.py and it is in the root).

Alternatively, it seems like it might be an issue with the logs. I double checked my permission and I have the ability to create logs with all resources. Any other ideas what the issue might be?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

requests library doesn't come by default in lambda. It looks like you are trying to import it in your function / library somewhere. To import it, you need the following line:

from botocore.vendored import requests

Alternatively, you would need to zip the requests library in the root of your zip file.

EDIT: There may be a dependency in one of your libraries that may need this. To overcome this, install requests in your application zip. To do this, run the following command in the root directory of your application: pip install requests -t ./.

A better way would be to create a file called requirements.txt and add all the dependencies in there. Use virtualenv to install all the packages defined in the requirements.txt using: pip install -r requirements.txt -t ./


UPDATE: Starting 10/21/19, the vendored version of the requests library in botocore will be removed. Refer this blog post for more details.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...