How to import Python dependencies in Serverless v1.0

烈酒焚心 提交于 2019-12-18 06:09:10

问题


Language: Python Framework: Serverless v1.0

Typically I would run pip freeze > requirements.txt in the project root

How can I get these dependencies packaged into every deploy?


回答1:


  1. create requirements.txt

    pip freeze > requirements.txt

  2. create a folder with all the dependencies:

    pip install -t vendored -r requirements.txt

Note that in order to use these dependencies in the code you'll need to add the following:

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

See https://stackoverflow.com/a/36944792/1111215 for another example.


UPDATE: Instead of the bullet (2) and the code above, you can now use the serverless-python-requirements plugin:

install the plugin

npm install --save serverless-python-requirements

and add the plugin to your serverless.yml

plugins:
  - serverless-python-requirements

Don't forget to make sure you have a requirements.txt file.

That's it, once sls deploy is called the plugin will package the dependencies with the code.

For a full sample take a look at the serverless-python-sample.




回答2:


I had similar problem, took these steps to deploy with dependencies. https://stackoverflow.com/a/41634501/2571060



来源:https://stackoverflow.com/questions/39774436/how-to-import-python-dependencies-in-serverless-v1-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!