I was building recently a serverless python microservice and for one of the endpoints i needed to implement jsonschema validation. Locally i have built it, covered with unit tests and everything was working just fine. But when i tried to deploy it to AWS Lambda with
serverless framework, i ended up with
"PackageNotFoundError: importlib_metadata" error. After couple of hours of trials and googling i wasn't able to figure out the solution ( there's not much information about this error on web). Thanks to one of my coleague i managed to finally fix it and the fix is stupid simple : in
serverless.yml file i have this block :
pythonRequirements: dockerizePip: non-linux slim: true fileName: requirements/base.txt
In order to make this error go away i just had to change
false!. It appears that the
slim option of
serverless-python-requirements plugin is indeed the culprit. The simple option is to turn it off entirely. But the plugin allows more fine grained control. See the config below:
slim: true slimPatternsAppendDefaults: false slimPatterns: - "**/*.pyc" - "**/*.pyo" - "**/__pycache__*"
This config will slim down by deleting all
.pyc/.pyo/__pycache__ files and dirs and by stripping the
"slimPatternsAppendDefaults=false". The root cause is that the default slim patterns also contain
"**/*.dist-info*". Removing these
dist-info/ dirs will break
importlib_metadata. So we can still use
slim, just not with that pattern.
So, in order to make this problem away just set your
serverless.yml like this :
pythonRequirements: dockerizePip: non-linux slim: false fileName: requirements/base.txt
Thanks @Freek Wiekmeijer for adding more light on this question in the comments.