Serverless Framework / Python for Lambda - packaging up local shared libraries

Viewed 157

Python package novice here. I'm working in Python and using the serverless framework for stack config and deployment. I have a file structure like this:

libs/
  __init__.py
  mylib.py
services/
  service1/
    handler.py

handler.py needs to reference mylib.py, so it includes it:

from libs.mylib include MyLib

My virtual env is set up to add the project root to PYTHONPATH, so this works on my local dev, but libs/mylib.py doesn't get included when I package it up for Lambda. How do I tell serverless to include the libs file in the package?

I've tried including a relative path in my requirements.txt file from both the services/service1/ path and from the root path - both have resulted in an error like this:

  STDERR: ERROR: Invalid requirement: './libs' (from line 1 of /var/task/requirements.txt)
  Hint: It looks like a path. File './libs' does not exist.

I've also tried including libs without a path, and that just installs the pypi package.

1 Answers

I had a similar problem a couple of days. The serverless-python-requirements was excluding the lib directory because it was outside of the module configuration I set.

The solution for me was to move the requirements.txt into the root directory and set up the module configuration also to the root.

Could you post your serverless file?

Related