Serverless Framework - Python and Requirements.txt
Solution 1
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.
Solution 2
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
Kurt Maile
Updated on July 09, 2022Comments
-
Kurt Maile almost 2 years
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).
However my resulting deployed function fails as it seems these dependencies are not installed as part of the packaging
'Unable to import module 'handler': No module named requests'
I assume it is serverless that does the pip install, but my resulting zip file is small and clearly its not doing it, either by design or my fault as I am missing something? Is it because its Lambda that does this? If so what am I missing?)
Is there documentation on what is required to do this and how it works? Is it serverless that pip installs these or on aws lambda side?
-
doorstuck over 7 yearsI don't think serverless runs pip. You have to run that yourself before you deploy your serverless project.
-
GWed about 6 yearsdetailed guide below stackoverflow.com/a/50027031/1085343
-
-
Gwen Au over 4 yearsIf you are having trouble with the
$ serverless deploy
command, you might have to remove the redundant node_modules that might have worked in the past as was in my case. I had to do the following commands: ``` $ rm -rf node_modules $ npm install serverless-python-requirements $ serverless deploy ``` -
Karl almost 2 years+1 for
source venv/bin/active
- wouldn't have thought it would have been an issue when deploying but apparently it is! Thanks