簡體   English   中英

AWS Lambda中的Python:“模塊'請求'沒有屬性'get'”

[英]Python in AWS Lambda: “module 'requests' has no attribute 'get'”

我目前正在嘗試在AWS Lambda函數中使用Python requests模塊。 以下是我到目前為止采取的步驟:

我創建了一個新目錄,並使用命令pip3 install requests -t .在其中安裝了請求模塊pip3 install requests -t .

然后我在目錄中編寫了一個簡單的Python腳本test_requests.py ,如下所示:

import requests
def my_handler(event, context):
    r = requests.get("http://google.com")
    return r

我使用zip test_requests.zip *壓縮整個目錄,包括請求模塊

然后,我使用以下命令將該函數上載到AWS: aws lambda create-function --function-name test_requests --zip-file fileb://test_requests.zip --handler test_requests.my_handler --runtime python3.6 --region us-east-1 --role xxxMY_ROLE_ARNxxx

最后,我用這個命令調用了這個函數: aws lambda invoke --function-name test-requests --payload {} --region us-east-1 lambda_response.txt

當我發出這個命令時,我從Lambda得到了一個未處理的異常。 輸出文件lambda_response.txt包含: {"errorMessage": "module 'requests' has no attribute 'get'", "errorType": "AttributeError", "stackTrace": [["/var/task/test_requests.py", 3, "my_handler", "r = requests.get('http://google.com')"]]}

我已經看到幾個關於AWS lambda的問題,並且無法正確導入模塊。 這些問題似乎都集中在lambda無法找到模塊。 在這種情況下,似乎lambda已找到請求,但無法訪問其所有屬性。

我弄清楚我做錯了什么。 zip test.zip *zip test.zip *目錄結構的頂層。 我需要-r標志才能捕獲所有內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM