如何将 python 库添加到 Alexa 的 AWS lambda 函数中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38877058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I add python libraries to an AWS lambda function for Alexa?
提问by Vineet Shah
I was following the tutorial to create an Alexa app using Python:
我正在按照教程使用 Python 创建一个 Alexa 应用程序:
I was able to successfully follow all the steps and get the app to work.I now want to modify the python code and use external libraries such as import requests
or any other libraries that I install using pip. How would I setup my lambda function to include any pip packages that I install locally on my machine?
我能够成功地遵循所有步骤并使应用程序运行。我现在想修改 python 代码并使用外部库,例如import requests
我使用 pip 安装的任何其他库。我将如何设置我的 lambda 函数以包含我在本地机器上安装的任何 pip 包?
采纳答案by Santhosh Gandhe
As it is described in the Amazon official documentation link hereIt is as simple as just creating a zip of all the folder contents after installing the required packages in your folder where you have your python lambda code.
正如此处的亚马逊官方文档链接中所描述的那样, 它就像在您拥有 python lambda 代码的文件夹中安装所需的包后创建所有文件夹内容的 zip 一样简单。
As Vineeth pointed above in his comment, The very first step in moving from an inline code editor to a zip file upload approach is to change your lambda function handler name under configuration settings to include the python script file name that holds the lambda handler.
正如上面 Vineeth 在他的评论中指出的那样,从内联代码编辑器转向 zip 文件上传方法的第一步是在配置设置下更改 lambda 函数处理程序名称,以包含保存 lambda 处理程序的 Python 脚本文件名。
lambda_handler => {your-python-script-file-name}.lambda_handler.
lambda_handler => {your-python-script-file-name}.lambda_handler。
Other solutions like python-lambdaand lambda-uploaderhelp with simplifying the process of uploading and the most importantly LOCAL TESTING. These will save a lot of time in development.
其他解决方案如python-lambda和lambda-uploader有助于简化上传过程和最重要的本地测试。这些将节省大量开发时间。
回答by dorian
The official documentationis pretty good. In a nutshell, you need to create a zip file of a directory containing both the code of your lambda function and all external libraries you use at the top level.
该官方文档是相当不错的。简而言之,您需要创建一个目录的 zip 文件,其中包含 lambda 函数的代码和您在顶层使用的所有外部库。
You can simulate that by deactivating your virtualenv, copying all your required libraries into the working directory (which is always in sys.path
if you invoke a script on the command line), and checking whether your script still works.
您可以通过停用 virtualenv、将所有需要的库复制到工作目录(sys.path
如果您在命令行上调用脚本时始终在工作目录中)并检查您的脚本是否仍然有效来模拟这一点。
回答by d3ming
You may want to look into using frameworks such as zappawhich will handle packaging up and deploying the lambda function for you.
您可能需要考虑使用zappa等框架,它会为您处理打包和部署 lambda 函数。
You can use that in conjunction with flask-askto have an easier time making Alexa skills. There's even a video tutorial of this (from the zappa readme) here
回答by jay
Echoing @d3ming's answer, a framework is a good way to go at this point. Creating the deployment package manually isn't impossible, but you'll need to be uploading your packages' compiled code, and if you're compiling that code on a non-linux system, the chance of running into issues with differences between your system and the Lambda function's deployed environment are high.
回应@d3ming 的回答,此时框架是一个很好的方法。手动创建部署包并非不可能,但您需要上传包的编译代码,如果您在非 Linux 系统上编译该代码,则可能会遇到系统差异问题并且 Lambda 函数的部署环境较高。
You can then work around that by compiling your code on a linux machine or Docker container.. but between all that complexity you can get much more from adopting a framework.
然后,您可以通过在 linux 机器或 Docker 容器上编译代码来解决这个问题……但是在所有这些复杂性之间,您可以从采用框架中获得更多。
Serverlessis well adopted and has support for custom python packages. It even integrates with Docker to compile your python dependencies and build the deployment package for you.
Serverless被广泛采用,并支持自定义 python 包。它甚至与 Docker 集成以编译您的 Python 依赖项并为您构建部署包。
If you're looking for a full tutorial on this, I wrote one for Python Lambda functions here.
如果您正在寻找关于此的完整教程,我在这里为 Python Lambda 函数编写了一个教程。
回答by Pedro
To solve this particular problem we're using a library called juniper. In a nutshell, all you need to do is create a very simple manifest file that looks like:
为了解决这个特殊问题,我们使用了一个名为juniper的库。简而言之,您需要做的就是创建一个非常简单的清单文件,如下所示:
functions:
# Name the zip file you want juni to create
router:
# Where are your dependencies located?
requirements: ./src/requirements.txt.
# Your source code.
include:
- ./src/lambda_function.py
From this manifest file, calling juni build
will create the zip file artifact for you. The file will include all the dependencies you specify in the requirements.txt.
从这个清单文件中,调用juni build
将为您创建 zip 文件工件。该文件将包含您在 requirements.txt 中指定的所有依赖项。
The command will create this file ./dist/router.zip
. We're using that file in conjunction with a sam template. However, you can then use that zip and upload it to the console, or through the awscli.
该命令将创建此文件./dist/router.zip
。我们将该文件与 sam 模板结合使用。但是,您随后可以使用该 zip 并将其上传到控制台,或通过 awscli。
回答by Vritansh Kamal
I too struggled for a while with this. The after deep diving into aws resources I got to know the lambda function on aws runs locally on a a linux. And it's very important to have the the python package version which matches with the linux version.
我也为此挣扎了一段时间。在深入研究 aws 资源后,我了解到 aws 上的 lambda 函数在 aa linux 上本地运行。并且拥有与 linux 版本匹配的 python 包版本非常重要。
You may find more information on this on : https://aws.amazon.com/lambda/faqs/
您可以在以下网址找到更多信息:https: //aws.amazon.com/lambda/faqs/
Follow the steps to download the version. 1. Find the .whl image of the package from pypi and download it on you local. 2. Zip the packages and add them as layers in aws lambda 3. Add the layer to the lambda function.
按照步骤下载版本。1.从pypi中找到包的.whl镜像,下载到本地。2. 压缩包并将它们作为层添加到 aws lambda 3. 将层添加到 lambda 函数。
Note: Please make sure that version you're trying to install python package matches the linux os on which the aws lambda performs computes tasks.
注意:请确保您尝试安装 python 包的版本与 aws lambda 执行计算任务的 linux 操作系统相匹配。
References : https://pypi.org/project/Pandas3/#files
回答by keithRozario
A lot of python libraries can be imported via Layers here: https://github.com/keithrozario/Klayers, or your can use a framework like serverless that has plugins to package packages directly into your artifact.
许多 python 库可以通过这里的层导入:https://github.com/keithrozario/Klayers,或者您可以使用像无服务器这样的框架,它具有插件将包直接打包到您的工件中。
回答by Morti
Amazon created a repository that deals with your situation: https://github.com/awsdocs/aws-lambda-developer-guide/tree/master/sample-apps/blank-python
亚马逊创建了一个处理您情况的存储库:https: //github.com/awsdocs/aws-lambda-developer-guide/tree/master/sample-apps/blank-python
The blank app is an example on how to push a lambda function that depends on requirements, with the bonus that being made by Amazon.
空白应用程序是一个关于如何推送依赖于需求的 lambda 函数的示例,亚马逊提供了奖励。
Everything you need to do is to follow the step by step, and update the repository based on your needs.
您需要做的一切就是按照步骤进行操作,并根据您的需要更新存储库。