【问题标题】:Unable to import grequest in AWS Lambda (Python 3.X)无法在 AWS Lambda (Python 3.X) 中导入 grequest
【发布时间】:2020-09-29 03:29:31
【问题描述】:

当我压缩“grequest”并尝试在 AWS Lambda 函数中使用它时,我收到此错误:

[ERROR] RuntimeError: Gevent is required for grequests.
Traceback (most recent call last):
  File "/var/lang/lib/python3.8/imp.py", line 234, in load_module
    return load_source(name, filename, file)
  File "/var/lang/lib/python3.8/imp.py", line 171, in load_source
    module = _load(spec)
  File "<frozen importlib._bootstrap>", line 702, in _load
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/var/task/fiveDay_updater.py", line 11, in <module>
    import grequests as asyncReq
  File "/var/task/grequests.py", line 18, in <module>
    raise RuntimeError('Gevent is required for grequests.')

似乎其他人也遇到过这个问题。 Unable to import grequests for AWS Lambda 不幸的是,上述链接中提出的解决方案对我不起作用。 有没有人有一个“grequest” zip 可以用于以后的 python,或者知道问题的解决方案?

【问题讨论】:

    标签: python amazon-web-services aws-lambda grequests


    【解决方案1】:

    我会推荐一种从未失败过的方法,它基于 docker 工具 lambci/lambda 并创建 lambda layers

    这已在以下链接中描述:

    为了验证,我创建了名为grequestslayer.zip 的层,如下所示:

    1. 创建文件夹layer并进入文件夹
    2. 在文件夹中,创建requirements.txt,内容为
    grequests
    
    1. 运行以下命令:
    docker run -v "$PWD":/var/task "lambci/lambda:build-python3.8" /bin/sh -c "pip install -r requirements.txt -t python/lib/python3.8/site-packages/; exit"
    
    1. 压缩创建的图层:
    zip -r grequestslayer.zip python
    

    图层将自动包含gevent

    python/lib/python3.8/site-packages/
    ├── bin
    ├── certifi
    ├── certifi-2020.6.20.dist-info
    ├── chardet
    ├── chardet-3.0.4.dist-info
    ├── easy_install.py
    ├── gevent
    ├── gevent-20.6.2.dist-info
    ├── greenlet-0.4.16.dist-info
    ├── greenlet.cpython-38-x86_64-linux-gnu.so
    ├── grequests-0.6.0.dist-info
    ├── grequests.py
    ├── idna
    ├── idna-2.10.dist-info
    ├── include
    ├── pkg_resources
    ├── __pycache__
    ├── requests
    ├── requests-2.24.0.dist-info
    ├── setuptools
    ├── setuptools-49.2.1.dist-info
    ├── urllib3
    ├── urllib3-1.25.10.dist-info
    ├── zope
    ├── zope.event-4.4.dist-info
    ├── zope.event-4.4-py2.7-nspkg.pth
    ├── zope.interface-5.1.0.dist-info
    └── zope.interface-5.1.0-py3.8-nspkg.pth
    
    1. 创建 lambda 层:

    1. 将图层添加到您的函数中:

    1. 在你的函数中使用grequests
    import json
    
    import grequests
    
    def lambda_handler(event, context):
        return {
            'statusCode': 200,
            'body': json.dumps('Hello from Lambda!')
        }
    
    

    【讨论】:

      【解决方案2】:

      我不确定您是否使用工具来部署功能,如果您不使用 linux 机器,生成 zip 会遇到一些问题。我对您的建议是使用其中一种工具在 AWS 上部署您的 python 函数。

      这些是我给你的建议,所以你不必手动处理 zip 生成,如果你不感兴趣,可能需要使用 linux 实例来生成你的 zip。

      【讨论】:

      • 那么什么是 Chalice 微框架?我不确定如何使用它..
      • 它是 AWS 提供的使用 Python 编写无服务器应用程序的框架。你只需要定义一个 virtualenv 然后安装库和你需要的所有库,这里有很好的例子github.com/aws/chalice
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-23
      • 1970-01-01
      • 2013-07-05
      • 2021-01-09
      • 2020-12-04
      • 1970-01-01
      相关资源
      最近更新 更多