简体   繁体   English

aws lambda 无法导入模块“lambda_function”:没有名为“requests”的模块

[英]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.我最近开始使用 AWS Lambda 对我编写的一些 python 代码使用触发器。 I currently have 2 lambda functions, both of which have been created with ZIP files.我目前有 2 个 lambda 函数,这两个函数都是用 ZIP 个文件创建的。 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.我到处搜索这个,答案是通过确保函数名称正确或确保 .zip 文件可读来解决的。 I have satisfied both of these conditions (the name of the file is lambda_function.py and it is in the root).我已经满足了这两个条件(文件的名称是 lambda_function.py,它在根目录中)。

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?任何其他想法可能是什么问题?

requests library doesn't come by default in lambda. requests库在 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.或者,您需要将requests库压缩到 zip 文件的根目录中。

EDIT: There may be a dependency in one of your libraries that may need this.编辑:您的某个库中可能存在需要此功能的依赖项。 To overcome this, install requests in your application zip.要解决这个问题, requests在您的应用程序 zip 中安装requests To do this, run the following command in the root directory of your application: pip install requests -t ./ .为此,请在应用程序的根目录中运行以下命令: pip install requests -t ./

A better way would be to create a file called requirements.txt and add all the dependencies in there.更好的方法是创建一个名为requirements.txt的文件并在其中添加所有依赖项。 Use virtualenv to install all the packages defined in the requirements.txt using: pip install -r requirements.txt -t ./使用 virtualenv 安装在 requirements.txt 中定义的所有包: pip install -r requirements.txt -t ./


UPDATE : Starting 10/21/19, the vendored version of the requests library in botocore will be removed.更新:从 19 年 10 月 21 日开始,botocore 中请求库的供应商版本将被删除。 Refer this blog post for more details.有关更多详细信息,请参阅此博客文章

Give it a check to this answer给它一个检查这个答案

If you're working with Python on AWS Lambda, and need to use requests , you better use urllib3 , it is currently supported on AWS Lambda and you can import it directly, check the example on urllib3 site.如果您在 AWS Lambda 上使用 Python,并且需要使用requests ,则最好使用urllib3 ,AWS Lambda 目前支持它,您可以直接导入它,请查看 urllib3 站点上的示例。

import urllib3

http = urllib3.PoolManager()
r = http.request('GET', 'http://httpbin.org/robots.txt')

r.data
# b'User-agent: *\nDisallow: /deny\n'
r.status
# 200

This will surely work.这肯定会奏效。 Just follow the steps:只需按照以下步骤操作:

Create a "python" directory inside any empty directory and pip install the modules there在任何空目录中创建一个“python”目录,然后在那里安装模块

mkdir lambda_layers
cd lambda_layers
mkdir python
cd python
pip install requests -t ./
cd ..
zip -r python_modules.zip .

If you want to have multiple modules in a single layer then install them inside the same 'python' directory that you have just created.如果你想在一个层中有多个模块,那么将它们安装在你刚刚创建的同一个“python”目录中。

Just make sure that you zip the "python" directory itself recursively with '-r'.只需确保使用“-r”递归压缩“python”目录本身。 That way lambda handler can locate the module in the default python version that you are using.这样 lambda 处理程序可以在您使用的默认 python 版本中找到模块。

Now you have your 'python_modules.zip' file with all the dependent modules inside.现在你有了包含所有依赖模块的“python_modules.zip”文件。 Go to the Layers of Lambda in the AWS console and create a layer uploading this zip file.转到 AWS 控制台中的 Lambda 层并创建一个上传此 zip 文件的层。 Choose the runtimes as per your python version that you are using in your lambda function, or you can select multiple python runtime versions.根据您在 lambda 函数中使用的 Python 版本选择运行时,或者您可以选择多个 Python 运行时版本。 Add this layer to your lambda function and you should be able to import your modules flawlessly.将此层添加到您的 lambda 函数,您应该能够完美地导入您的模块。

https://aws.amazon.com/blogs/developer/removing-the-vendored-version-of-requests-from-botocore/ https://aws.amazon.com/blogs/developer/removing-the-vendored-version-of-requests-from-botocore/

AWS removed the vendored version of requests from Botocore. AWS 从 Botocore 中删除了供应商版本的requests

Steps:步骤:

  1. cmd >> pip install requests cmd >> pip install requests

  2. Python code:蟒蛇代码:

     import requests response = requests.get('https://...')

So this is what resolved in my case所以这就是在我的情况下解决的问题

在此处输入图片说明

Handler :: "yfinance_lamdba.lambda_handler"处理程序::“yfinance_lamdba.lambda_handler”

In the above line在上面的行中

               **yfinance_lamdba** is filename 'yfinance_script.py'

               **lambda_handler** is function 'def lambda_handler' which has actual code

step1- mkdir python step1- mkdir python

step2- pip3 install -t python requests step2- pip3 install -t python requests

step3- zip python folder step3-压缩python文件夹

step4- create new layer in aws lambda function and upload this zip file步骤 4- 在 aws lambda 函数中创建新层并上传此 zip 文件

step5- add this layer in your lambda function step5- 在你的 lambda 函数中添加这一层

Unable to import module 'lambda_function': No module named 'requests'

Please try using python3.7 as a runtime.请尝试使用 python3.7 作为运行时。 It will fix the requests issue!它将解决请求问题!

try using the wheel files, it will work.尝试使用轮文件,它会起作用。

    'https://pypi.org/project/pytz/#files'
    'https://pypi.org/project/pandas/#files'
    'https://pypi.org/project/numpy/#files'

Need to unzip locally and merge these files then zip it before uploading to AWS Lambda function.需要在本地解压缩并合并这些文件,然后在上传到 AWS Lambda 函数之前将其压缩。

This usually happens if you had missed something to add to your lambda Layers.如果您错过了要添加到 lambda 图层的内容,通常会发生这种情况。 I tried replicating one of my lambda functions and forgot to import the lambda layer that I was having in my old lambda function, so after importing the file into layers, the API started working fine.我尝试复制我的 lambda 函数之一,但忘记导入我在旧 lambda function 中的 lambda 层,因此在将文件导入层后,API 开始正常工作。 May be this might help anyone.可能这对任何人都有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法导入模块“lambda_function”:没有名为“psycopg2._psycopg aws lambda 函数”的模块 - Unable to import module 'lambda_function': No module named 'psycopg2._psycopg aws lambda function AWS Lambda 层无法导入模块“lambda_function”:没有名为“pyarrow.lib”的模块 - AWS Lambda Layer Unable to import module 'lambda_function': No module named 'pyarrow.lib' 无法导入模块“lambda_function”:没有名为“pymongo”的模块 - Unable to import module 'lambda_function': No module named 'pymongo' AWS Lambda - 无法导入模块“lambda_function” - AWS Lambda - unable to import module 'lambda_function' 无法导入模块“lambda_function”: - Unable to import module 'lambda_function': 无法导入模块“lambda_function”:没有名为“flatten_json”的模块 - Unable to import module 'lambda_function': No module named 'flatten_json' AWS Lambda Opencv(“无法导入模块‘lambda_function’:libgthread-2.0.so.0:无法打开共享对象文件:没有这样的文件或目录”) - AWS Lambda Opencv ("Unable to import module 'lambda_function': libgthread-2.0.so.0: cannot open shared object file: No such file or directory") 无法导入模块“lambda_function”:/lib64/libc.so.6:找不到版本“GLIBC_2.18” - Unable to import module 'lambda_function': /lib64/libc.so.6: version `GLIBC_2.18' not found AWS 中没有名为“MySQLdb”的模块 Lambda function - No module named 'MySQLdb' in AWS Lambda function AWS Lambda 导入 psycopg2 - 无法导入模块“app”:没有名为“psycopg2._psycopg”的模块 - AWS Lambda Importing psycopg2 - Unable to import module 'app': No module named 'psycopg2._psycopg
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM