[英]AWS Lambda Python3.7 Function - numpy: cannot import name 'WinDLL'
I have a function set up in lambda that runs a python script from a .zip file.我在lambda中设置了一个函数,该函数从.zip文件运行 python 脚本。 I have created a
virtualenv
and included all of the necessary packages in the .zip file (from the Lib\\site-packages
folder).我创建了一个
virtualenv
并在.zip文件(来自Lib\\site-packages
文件夹)中包含了所有必要的包。
Below are the import statements for the packages used in the script:以下是脚本中使用的包的导入语句:
import requests
import boto3
import logging
import os
from botocore.exceptions import ClientError
from pprint import pprint
import pandas as pd
from datetime import datetime
import s3fs
When I attempt to run the lambda function I am receiving the following error:当我尝试运行lambda 函数时,我收到以下错误:
START RequestId: e302cee0-3c51-453a-84c1-6eb1f9c123a0 Version: $LATEST
[ERROR] Runtime.ImportModuleError: Unable to import module 'export-dev': Unable to import required dependencies:
numpy: cannot import name 'WinDLL' from 'ctypes' (/var/lang/lib/python3.7/ctypes/__init__.py)
END RequestId: e302cee0-3c51-453a-84c1-6eb1f9c123a0
REPORT RequestId: e302cee0-3c51-453a-84c1-6eb1f9c123a0 Duration: 1.65 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 70 MB
I do not use the ctypes
, WinDLL
or any related packages explicitly in my code.我没有在我的代码中明确使用
ctypes
、 WinDLL
或任何相关的包。
Aws lambda will throw you an error if you don't have the correct version of dependencies packaged with your code, which may depend on the OS (lambda runs on linux) and the python version.如果您的代码中没有打包正确版本的依赖项,aws lambda 会向您抛出错误,这可能取决于操作系统(lambda 在 linux 上运行)和 python 版本。
Based on your requirements, it's pandas throwing you the error.根据您的要求,是熊猫向您抛出错误。 To run pandas on lambda, you need to include the following packages:
要在 lambda 上运行 pandas,您需要包含以下包:
pandas - code compiled for the linux, which is what lambda runs you. pandas - 为 linux 编译的代码,这是 lambda 运行你的。 You can find it here https://pypi.org/project/pandas/#files download the 'manylinux' version of the .whl file, that matches your python lambda version.
您可以在此处找到它https://pypi.org/project/pandas/#files下载与您的 python lambda 版本匹配的 .whl 文件的“manylinux”版本。
eg if you are running py3.7, then get pandas-0.25.3-cp37-cp37m-manylinux1_x86_64.whl例如,如果您正在运行 py3.7,则获取 pandas-0.25.3-cp37-cp37m-manylinux1_x86_64.whl
Unzip the contents of the .whl file into the root folder of your lambda folder.将 .whl 文件的内容解压缩到 lambda 文件夹的根文件夹中。 This is the library version that lambda needs
这是 lambda 需要的库版本
Note for pandas 0.25+, you also need to include the pytz package as well, see note below on requests熊猫 0.25+ 的注意事项,您还需要包括 pytz 包,请参阅下面的请求注意事项
numpy - You can now get in lambda (tested for py3.7) through installing a 'layer' through the lambda console, see screenshots below. numpy - 您现在可以通过 lambda 控制台安装“层”进入 lambda(已针对 py3.7 进行测试),请参阅下面的屏幕截图。
Side note on requests关于请求的旁注
Notice that the package here https://pypi.org/project/requests/#files only have a 'none-any' version, that means the source doesn't need to be compiled, so you can safely include the version you got from pip注意这里的包https://pypi.org/project/requests/#files只有一个“none-any”版本,这意味着源代码不需要编译,所以你可以安全地包含你得到的版本来自点子
this applies to the pytz dependency of pandas as well这也适用于熊猫的 pytz 依赖
Screenshots installing layers in aws console在 aws 控制台中安装层的屏幕截图
Since numpy is written in C you should build it for a linux distribution.由于 numpy 是用 C 编写的,因此您应该为 linux 发行版构建它。 I recommend you using the serverless framework because it will simplify your life a lot when you are using a Windows laptop.
我建议您使用无服务器框架,因为当您使用 Windows 笔记本电脑时,它会大大简化您的生活。
Install the serverless framework and make sure you have docker安装无服务器框架并确保你有docker
go to the root of your project and execute:转到项目的根目录并执行:
sls create --template aws-python
install the plugin for deploying python apps:安装用于部署 python 应用程序的插件:
serverless plugin install -n serverless-python-requirements
in your serverless.yml
file add:在
serverless.yml
文件中添加:
plugins:
- serverless-python-requirements
custom:
pythonRequirements:
dockerizePip: non-linux
make sure you adjust the path to your lambda function确保调整 lambda 函数的路径
functions:
hello:
handler: handler.hello
deploy with the correct libraries using使用正确的库进行部署
sls deploy
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.