6

语言:Python 框架:Serverless v1.0

通常我会pip freeze > requirements.txt在项目根目录中运行

如何将这些依赖项打包到每个部署中?

4

2 回答 2

12
  1. 创造requirements.txt

    点冻结> requirements.txt

  2. 创建一个包含所有依赖项的文件夹:

    pip install -t vendored -r requirements.txt

请注意,为了在代码中使用这些依赖项,您需要添加以下内容:

import os
import sys
here = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.join(here, "./vendored")) 

有关另一个示例,请参见https://stackoverflow.com/a/36944792/1111215


更新:您现在可以使用serverless-python-requirements插件,而不是项目符号 (2) 和上面的代码:

安装插件

npm install --save serverless-python-requirements

并将插件添加到您的serverless.yml

plugins:
  - serverless-python-requirements

不要忘记确保你有一个requirements.txt文件。

就是这样,一旦sls deploy被调用,插件就会将依赖项与代码一起打包。

有关完整示例,请查看serverless-python-sample

于 2016-09-30T12:35:44.917 回答
0

我有类似的问题,采取了这些步骤来部署依赖项。 https://stackoverflow.com/a/41634501/2571060

于 2017-01-13T12:22:07.930 回答