【问题标题】:Missing required dependencies ['numpy'] in AWS Lambda after installing numpy into directory, how to fix?将 numpy 安装到目录后,AWS Lambda 中缺少必需的依赖项 ['numpy'],如何解决?
【发布时间】:2018-11-01 09:45:03
【问题描述】:

我正在尝试将我的 python 代码上传到 AWS Lambda。我一直在按照本指南创建部署包 (https://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html)。

我在桌面上创建了一个文件夹“project-dir”,并将我的 python 文件“Twilio_Alerts_AWS.py”移动到该文件夹​​中。我已经使用了命令:

pip install module-name -t /path/to/project-dir

将我所有的库安装到文件夹中。接下来,我突出显示所有内容并通过右键单击文件夹中突出显示的文件点击“压缩”。这会生成一个名为“存档”的压缩文件

我将'archive.zip' 放在 AWS 上的 S3 存储桶中,并将其调用到 AWS Lambda 中。即使我已将 numpy 安装到文件夹中,我仍然收到错误 Unable to import module 'Twilio_Alerts_AWS': Missing required dependencies ['numpy']

不知道我做错了什么。

我正在尝试上传的代码:

from twilio.rest import Client
import time
import datetime
import requests
import pandas as pd
from pandas.io.json import json_normalize




def lambda_handler(event, context):
    # Your Account SID from twilio.com/console
    account_sid = "xxx"
    # Your Auth Token from twilio.com/console
    auth_token  = "xxx"

    client = Client(account_sid, auth_token)


    current_datetime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')


    headers = {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'x-api-key': 'xxx',
        'x-organization-id': 'xxx',
        'x-facility-id': 'xxx',
        'x-user-id': 'xxx',
    }



    orders_staging_api_call = requests.get('URL', headers=headers, verify=False)
    consumers_staging_api_call = requests.get('URL', headers=headers, verify=False)
    inventory_staging_api_call = requests.get('URL', headers=headers, verify=False)

    lst = ["+1234567890"]

    ##Consumers API Alert
    if consumers_staging_api_call.status_code !=200:
        for i in lst:
            message = client.messages.create(
                    to=i, 
                    from_="+1234567890",
                    body="API connection between A and B has failed for: Consumers.Datetime of check:{}".format(current_datetime))
            time.sleep(5)
        print(message.sid)
    else:
        print('done')

编辑: 使用 osx 机器。

【问题讨论】:

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


    【解决方案1】:

    AWS 文档的建议是针对某些依赖项使用 .whl 文件。

    您可以从 python 项目下载文件中解包 numpy .whl 文件,这里有更完整的答案

    Pandas in AWS lambda gives numpy error - Answer

    【讨论】:

      【解决方案2】:

      这是仅在 AWS lambda 中提供 Numpy 的一种简短方法:只需将 scipy-numpy 层(从 ​​Amazon 提供的 publicy)添加到您的 lambda 函数(在 AWS Lambda 中:层 -> 添加层 -> numpy scipy 层应该已经得到建议)。

      如果您对需要编译的几个包有问题,我想向一个有用的 docker 容器添加提示,该容器可用于为 Linux 编译包: https://hub.docker.com/r/lambci/lambda/

      还有其他解决编译问题的解决方案,如果您在项目中使用无服务器,则一种是 serverless-python-requirements npm-package。 但是我经历过,如果您在具有无服务器和无服务器 python 要求的自定义 gitlab-runner 中运行无服务器部署命令(用于 ci/cd 目的),这将不起作用。 在这种情况下,我目前正在使用 AWS Lambda 层来提供所需的依赖项。这是为 pandas 创建层的一个很好的解释:https://medium.com/@qtangs/creating-new-aws-lambda-layer-for-python-pandas-library-348b126e9f3e

      我的回答可能太过分了,但上述选项可能对其他来到这里的读者有用。

      【讨论】:

        【解决方案3】:

        这里的回答对我有帮助:Pandas in AWS lambda gives numpy error

        TLDR:在 mac 上编译的库在 linux 上不起作用,因此您需要确保以一种或另一种方式获取 linux 版本(例如 Docker)。

        【讨论】:

        • Windows 也一样吗??
        猜你喜欢
        • 2019-05-05
        • 1970-01-01
        • 1970-01-01
        • 2017-07-01
        • 1970-01-01
        • 2017-06-11
        相关资源
        最近更新 更多