无法导入模块"lambda_function":没有名为"pandas"的模块 [英] Unable to import module 'lambda_function': No module named 'pandas'
问题描述
START RequestId:3d5691d9-ad79-4eed-a26c-5bc3f1a23a99版本:$ LATEST 无法导入模块"lambda_function":没有名为"pandas"的模块
END RequestId:3d5691d9-ad79-4eed-a26c-5bc3f1a23a99
START RequestId: 3d5691d9-ad79-4eed-a26c-5bc3f1a23a99 Version: $LATEST Unable to import module 'lambda_function': No module named 'pandas'
END RequestId: 3d5691d9-ad79-4eed-a26c-5bc3f1a23a99
我使用64位Windows 7作为主机操作系统.
I'm using Windows 7 64-bit as the host OS.
我只是想在AWS-Lambda环境中使用熊猫.就像我在Windows环境中使用它一样,我正在寻找Lambda的简单解决方案.
I simply want to use pandas in AWS-Lambda environment. Just like I use it in windows environment, I am looking for a simple solution for Lambda.
- 在虚拟盒子上安装了Xubuntu.
- 在virtual-box的Xubuntu中创建一个名为
myvenv
的虚拟环境. - 然后我在
myvenv
中安装了pandas3.6. - 此后,我将
myvenv
中位于位置'/usr/local/lib/python3.6/site-packages/'
的文件夹内容复制到了主机OS. - 在主机操作系统(Windows 7)中,我创建了一个名为
packs
的文件夹,粘贴了myvenv
的内容. - 在主机操作系统(Windows 7)中的
packs
中创建了lambda_function.py
脚本 - 然后我使用7zip软件压缩文件夹
packs
,并将其作为zip
上载到Lambda
- 在Lambda中,lambda函数处理程序名称为
lambda_handler()
.该代码段如下所示:
- Installed Xubuntu on a virtual box.
- Create a virtual environment called
myvenv
in Xubuntu on virtual-box. - Then I installed pandas3.6 in
myvenv
. - Thereafter, I copied the folder contents in
myvenv
at location'/usr/local/lib/python3.6/site-packages/'
to my host OS. - In the host OS (windows 7), I created a folder called
packs
, pasted the contents ofmyvenv
. - created a
lambda_function.py
script inpacks
in host OS (windows 7) - I then zipped the folder
packs
using 7zip software and upload it aszip
inLambda
- In Lambda, the lambda function handler name is,
lambda_handler()
. The code snippet looks like,
将熊猫作为pd导入
import pandas as pd
def lambda_handler(event, context):
dates = pd.date_range('2019001', periods=6)
df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list('ABCD'))
print(df)
- 该处理程序的名称为
lambda_function.lambda_handler
.我已授予lambda-roleAWSLambdaFullAccess
权限. - 超时设置为4分3秒.
-
测试事件看起来像
- The handler is named as
lambda_function.lambda_handler
. I have given the lambda-roleAWSLambdaFullAccess
permission. - The time out is set to 4 min and 3 sec.
The test event looks like
{ "key1":如果一切正常,将打印此内容" }
{ "key1": "This will be printed if all OK" }
我尝试了以下解决方案:
I have tried the following solutions:
- 尝试对pandas&进行预编译的linux兼容二进制文件来自此处的numpy –祝您好运.
- 在Lambda中,将
Handler info
更改为 python_filename.function_name .就我而言,这是 lambda_function.lambda_handler -失败,没有名为"pandas"的模块错误. - 将lambda函数放在根文件夹中,使用7zip软件压缩该文件夹,然后将该文件夹上载到S3存储桶.就我而言,我将函数放置在位置
python\lib\python3.6\site_packages\lambda_function.py
失败,没有名为"pandas"的模块错误. - 已经尝试过在SO 1 中发布的这些相关解决方案, 2 , 3 , 5 , 6
- Tried precompiled linux-compatible binaries for pandas & numpy from here -- no luck.
- In Lambda, changed the
Handler info
to python_filename.function_name. For my case, it was lambda_function.lambda_handler -- failed with no module named 'pandas' error. - placed the lambda function in the root folder, zipped the folder using 7zip software and upload the folder to the S3 bucket. For my case, I placed the function at location
python\lib\python3.6\site_packages\lambda_function.py
failed with no module named 'pandas' error. - Already tried these related solutions posted on SO, 1, 2, 3, 4, 5, 6
注意:我不想使用Docker,因为我不知道如何使用它,而且由于现在我很生气,所以我不愿意学习它.我来自Windows环境(很烂,我现在知道.)
Note: I do not want to use Docker, because I do not know how to use it and I'm not willing to learn it as I'm exasperated now. I'm coming from a windows environment (it sucks, I now know.)
有关如何使其正常工作的任何想法.
Any ideas on how to get this to work.
推荐答案
我能够使用Lambda层和
Amazon linux
Cloud 9实例成功导入熊猫库.有一些我在Cloud 9实例中执行的命令以及Lambda函数的输出.我必须稍稍更改代码,因为它由于导入错误和字符串值错误而失败.I was able to import the pandas library successfully using a Lambda layer and an
Amazon linux
Cloud 9 instance. There are the commands I executed in the Cloud 9 instance and the Lambda function's output. I had to change the code slightly since it was failing with an import error and string value error.或者,这些命令也可以在EC2实例中执行.如果无法使用 AWS Lambda当前使用.我认为在这里使用ubuntu实例不可行.
Alternatively, these commands can also be executed in an EC2 instance. If it's not possible to use the SAM CLI(which uses docker) or just plain docker on windows we'll need to use an
Amazon Linux
instance to build everything since that's what AWS Lambda uses currently. I don't believe using an ubuntu instance will work here.命令:
python --version Python 3.6.8 # https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html # python 3.6 uses Amazon Linux currently mkdir project cd project virtualenv v-env source ./v-env/bin/activate pip install pandas deactivate # creating layer # https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-path mkdir python cd python cp -r ../v-env/lib64/python3.6/dist-packages/* . cd .. zip -r panda_layer.zip python aws lambda publish-layer-version --layer-name pandas --zip-file fileb://panda_layer.zip --compatible-runtimes python3.6
publish-later-version 命令将在其中创建一个新的 AWS Lambda层命令或配置文件对于CLI.
The publish-later-version command will create a new AWS Lambda layer in the region given in the command or the config file for the CLI.
Lambda层会将库应用于Lambda函数的代码,而无需直接将其应用于部署包.这也允许在Lambda中使用在线代码编辑器,因为部署程序包位于
A Lambda layer will apply the library to the Lambda function's code without needing to apply it directly to the deployment package. This also allows the use of the online code editor in Lambda since the deployment package is under the limit of 3MB
我通过单击Web控制台中的图层"按钮并选择我最近发布的图层版本来应用Lambda图层.我在那里有第二个版本,因为第一次尝试将它放置在
lib
目录的内容中,该目录不是用于64位操作系统的,并且我的代码在AWS Lambda中失败.I applied the Lambda layer by clicking on the Layer button in the web console and choosing the layer version that I most recently published. I have a second version there because the first time I attempted this is put the contents of the
lib
directory which isn't for a 64 bit OS and my code failed in AWS Lambda.或者,您也可以使用CLI命令更新功能配置
Alternatively, you can also apply the layer using the CLI command update-function-configuration
我使用的Lambda函数代码:
import pandas as pd import numpy as np def lambda_handler(event, context): dates = pd.date_range(start='1/1/2018', end='1/08/2018') df = pd.DataFrame(np.random.randn(8, 4), index=dates, columns=list('ABCD')) print(str(df))
Lambda输出:
START RequestId: 27f09b6c-a4cd-49dd-bb3e-ae6fc7cd5850 Version: $LATEST A B C D 2018-01-01 -1.040318 0.450841 -0.381687 -0.105480 2018-01-02 -1.381793 -0.481572 0.828419 -0.885205 2018-01-03 1.437799 -0.649816 -0.577112 0.400670 2018-01-04 -0.730997 -0.778775 -1.514203 1.165661 2018-01-05 1.963595 -1.137054 0.920218 0.960210 2018-01-06 -0.429179 -0.745549 1.482562 0.298623 2018-01-07 -1.082388 -0.529476 -1.051663 1.616683 2018-01-08 0.042779 -2.338471 -0.142992 0.680399 END RequestId: 27f09b6c-a4cd-49dd-bb3e-ae6fc7cd5850 REPORT RequestId: 27f09b6c-a4cd-49dd-bb3e-ae6fc7cd5850 Duration: 536.76 ms Billed Duration: 600 ms Memory Size: 128 MB Max Memory Used: 122 MB Init Duration: 1721.51 ms XRAY TraceId: 1-5d741e40-1311daa29fc16c74735988fc SegmentId: 61a595dd3492c331 Sampled: false
这篇关于无法导入模块"lambda_function":没有名为"pandas"的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- The handler is named as