"errorMessage":“无法导入模块'lambda_function'"; [英] "errorMessage": "Unable to import module 'lambda_function'"

查看:118
本文介绍了"errorMessage":“无法导入模块'lambda_function'";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要使用numpy,pandas等python依赖项在AWS中部署lambda函数. 制作所有zip文件并使用S3路径执行后,发现以下错误

Need to deploy lambda function in AWS using python dependencies like numpy, pandas. after making zip file of all and using with S3 path to execute found following error

无法导入模块"lambda_function": 导入多数组numpy扩展模块失败.最多 可能您正在尝试导入失败的numpy版本. 如果您使用的是numpy git repo,请尝试git clean -xdf(删除所有 文件不受版本控制).否则,请重新安装numpy.

Unable to import module 'lambda_function': Importing the multiarray numpy extension module failed. Most likely you are trying to import a failed build of numpy. If you're working with a numpy git repo, try git clean -xdf (removes all files not under version control). Otherwise reinstall numpy.

最初的错误是:无法导入名称多数组

Original error was: cannot import name multiarray

推荐答案

经过大量的努力,我发现必须从python3.6 virtualenv内创建部署包 ,而不是直接从主机.我在Ubuntu 16.04 docker映像中执行了以下操作.假设您已经安装/配置了python3.6,virtualenv和awscli,并且您的lambda函数代码位于~/lambda_code目录中:

After much effort, I found that I had to create my deployment package from within a python3.6 virtualenv, rather than directly from the host machine. I did the following within a Ubuntu 16.04 docker image. This assumes that you have python3.6, virtualenv and awscli already installed/configured, and that your lambda function code is in the ~/lambda_code directory:

1)cd ~(我们将在主目录中构建virtualenv)

1) cd ~ (We'll build the virtualenv in the home directory)

2)virtualenv venv --python=python3.6(创建虚拟环境)

2) virtualenv venv --python=python3.6 (Create the virtual environment)

3)source venv/bin/activate(激活虚拟环境)

3) source venv/bin/activate (Activate the virtual environment)

4)pip install numpy

5)cp -r ~/venv/lib/python3.6/site-packages/* ~/lambda_code(将所有已安装的软件包复制到lambda_code目录的根目录下.这将包含一些不必要的文件,但是您可以根据需要自行删除这些文件)

5) cp -r ~/venv/lib/python3.6/site-packages/* ~/lambda_code (Copy all installed packages into root level of lambda_code directory. This will include a few unnecessary files, but you can remove those yourself if needed)

6)cd ~/lambda_code

7)zip -r9 ~/package.zip .(压缩lambda程序包)

7) zip -r9 ~/package.zip . (Zip up the lambda package)

8)aws lambda update-function-code --function-name my_lambda_function --zip-file fileb://~/package.zip(上传到AWS)

8) aws lambda update-function-code --function-name my_lambda_function --zip-file fileb://~/package.zip (Upload to AWS)

您的lambda函数现在应该可以正常运行了.

Your lambda function should now be able to import numpy with no problems.

如果您想使用更现成的解决方案,则可以考虑使用无服务器部署lambda函数.在找到上述解决方案之前,我已按照指南此处并能够运行numpy在python3.6 lambda函数中成功.

If you want a more out-of-the-box solution, you could consider using serverless to deploy your lambda function. Before I found the above solution, I followed the guide here and was able to run numpy successfully in a python3.6 lambda function.

这篇关于"errorMessage":“无法导入模块'lambda_function'";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆