How do I deploy a function in python with its dependencies?

故事扮演 提交于 2019-12-01 17:07:24

Here are a few steps that should make it work:

  1. Make sure that the handler entry in s-function.json has the function-name in its path: "handler": "function-name/handler.handler",
  2. in handler.py add the following:

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

That's it. Let me know if it worked.

I'd recommend using the serverless-python-requirements plugin to include packages installed via pip

I followed below steps to deploy with dependencies:

  • Created a directory for dependencies in project root mkdir .vendor

  • Add dependencies in requirements.txt file manually or use pip freeze > requirements.txt

  • Update serverless.yml file

package: include: - .vendor/**

  • Include .vendor directory to system path in handler.py file

import sys sys.path.insert(0, './.vendor')

  • Install dependencies pip install -r requirements.txt -t .vendor

Now serverless deploy will upload function with dependencies.

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