How to import Python dependencies in Serverless v1.0

Language: Python Framework: Serverless v1.0

I usually ran pip freeze > requirements.txt in the root of the project

How can I get these dependencies packaged in each deployment?

+3
source share
2 answers
  • create requirements.txt

    pip freeze> requirements.txt

  • create a folder with all the dependencies:

    pip install -t vendored -r requirements.txt

Please note that to use these dependencies in your code, you 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 fooobar.com/questions/718309 / ... for another example.


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

install plugin

 npm install --save serverless-python-requirements 

and add the plugin to your serverless.yml

 plugins: - serverless-python-requirements 

Remember to make sure you have a requirements.txt file.

So that as soon as sls deploy is called, the plugin will pack dependencies with the code.

For a complete example, take a look at serverless-python-sample .

+11
source

I had a similar problem, took the following steps to deploy with dependencies. fooobar.com/questions/718309 / ...

+1
source

Source: https://habr.com/ru/post/1268479/


All Articles