Serverless Framework - Python and Requirements.txt

后端 未结 3 1998
南方客
南方客 2020-12-16 00:10

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).

相关标签:
3条回答
  • 2020-12-16 00:46

    Now you can use serverless-python-requirements. It works both for pure Python and libraries needing native compilation (using Docker):

    A Serverless v1.x plugin to automatically bundle dependencies from requirements.txt and make them available in your PYTHONPATH.

    Requires Serverless >= v1.12

    0 讨论(0)
  • 2020-12-16 01:03

    The Serverless Framework doesn't handle the pip install. See https://stackoverflow.com/a/39791686/1111215 for the solution

    0 讨论(0)
  • 2020-12-16 01:06

    You need to install serverless-python-requirements and docker

    $ npm install serverless-python-requirements
    

    Then add the following to your serverless.yml

    plugins:
       - serverless-python-requirements
    
    custom:
      pythonRequirements:
         dockerizePip: non-linux
    

    Make sure you have your python virtual environment active in CLI:

    $ source venv/bin/activate
    

    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
    

    Make sure you have opened docker then deploy serverless as normal

    $ serverless deploy
    

    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.

    Full guide here

    0 讨论(0)
提交回复
热议问题