问题描述
使用无服务器框架v1.0.0,我在服务根目录中有一个"requirements.txt",其内容为相关python软件包的列表. (例如请求).
Using the serverless framework v1.0.0, I have a 'requirements.txt' in my service root with the contents being the list of dependant python packages. (e.g. requests).
但是我得到的部署功能失败了,因为这些依赖关系似乎没有作为包装的一部分安装
However my resulting deployed function fails as it seems these dependencies are not installed as part of the packaging
无法导入模块'处理程序':没有名为请求的模块"
'Unable to import module 'handler': No module named requests'
我假设pip可以安装在无服务器上,但是我得到的zip文件很小,很明显它没有这样做,无论是设计还是我的错,因为我缺少某些东西?是因为它的Lambda能够做到这一点吗?如果是这样,我想念什么?)
I assume it is serverless that does the pip install, but my resulting zip file is small and clearly its not doing it, either by design or my fault as I am missing something? Is it because its Lambda that does this? If so what am I missing?)
是否有关于执行此操作所需的文件以及其工作方式的文档? pip是将这些安装在无服务器还是在lambda一侧?
Is there documentation on what is required to do this and how it works? Is it serverless that pip installs these or on aws lambda side?
推荐答案
您需要安装serverless-python-requirements
和 docker
$ npm install serverless-python-requirements
然后将以下内容添加到您的serverless.yml
Then add the following to your serverless.yml
plugins:
- serverless-python-requirements
custom:
pythonRequirements:
dockerizePip: non-linux
确保在CLI中激活了python虚拟环境:
Make sure you have your python virtual environment active in CLI:
$ source venv/bin/activate
使用pip安装任何依赖项-请注意,在CLI中,您可以通过终端文本左侧的venv
来判断venv是否处于活动状态
Install any dependencies with pip - note that in CLI you can tell if venv is active by the venv
to the left of the terminal text
(venv) $ pip install <NAME>
(venv) $ pip freeze > requirements.txt
确保已打开docker,然后照常部署无服务器
Make sure you have opened docker then deploy serverless as normal
$ serverless deploy
将发生的情况是serverless-python-requirements
将使用lambda环境在docker中为您构建python软件包,然后将其压缩以准备与其他代码一起上传.
What will happen is that serverless-python-requirements
will build you python packages in docker using a lambda environment, and then zip them up ready to be uploaded with the rest of your code.
这篇关于无服务器框架-Python和Requirements.txt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!