python project with local packages
This fit AWS Lambda function deployment
If we need to deploy python AWS Lambda functions, AWS requires you to zip all the packages into the .zip file then upload. There are multiple options here:
Option1
Install packages into a virtual environment (you can use python venv command or pipenv). When you zip files, move all your codes into the venv folders, then zip them together.
Good:
Don't need to add path to tell python where to import packages, since project root is automatically one of import path for python.
Using venv is a natural way of manage project.
Bad:
It's not easy to setup CI/CD process to deploy.
All your codes and installed packages are in the same root, very hard to distinguish.
Option2
Install packages into project root (pip install -r requirements.txt -t .). When you zip files, zip the whole project files (exclude some useless files).
Good:
Don't need to add path to tell python where to import packages, since project root is automatically one of import path for python.
Easy to setup CI/CD process
Bad:
All your codes and installed packages are in the same root, very hard to distinguish.
Option3
if you use serverless framework, you can use Serverless Python Requirements plugin. It's easy, but its approach mechanism is just option1, not changed.
Good:
Easy to config.
Bad:
It need to install docker if you need to build native packages that are part of your dependencies like Psycopg2, NumPy, Pandas, etc.
When you deploy to AWS with this approach, you will find installed packages and your codes are all in the project root as well.
Option4 (Applied)
Install packages into project sub-folder such as packages ( pip install -r requirements.txt -t packages).
Python don't know it can import packages from this folder, so we need to add it to sys path manually. I found a easier way to achieve it: to add a python file packages_load.py :
and in every python file in the project, just import it at head: import packages_load
When you zip files, zip the whole project files (exclude some useless files).
Pylint + VSCode issue
There is a small problem that if you use pylint + VSCode: it will show error on importing (Unable to import XXX). Also, autocomplete for packages doesn't works in VSCode. To solve the problems, need to change .vscode/settings.json:
Gitlab CI/CD script using serverless framework
example of .gitlab-ci.yml:
Good:
Easy to setup CI/CD process
Your codes and dependent packages are separated as packages are all in
packagessub-folder.
Bad:
Need to add
packagessub-folder into sys path.Need to do extra settings to work with
pylintandVSCode.
Last updated
Was this helpful?