serverless/serverless-python-requirements

[Request for help] Python project structure with src-folder working?

klintan opened this issue · 2 comments

I have tried for 2 days now to get a python poetry project with src folder to work (using poetry new --src my-package for instance)

my-package
├── serverless.yml
├── pyproject.toml
├── README.md
├── src
│   └── my_package
│       └── __init__.py
└── tests
    └── __init__.py

I've tried endless options / conigurations, and I have projects in the past that works, so I'm wondering if I'm going crazy or something changed?
Rather than listing all the different configurations I've gone through if someone could either point to a serverless.yml file + project structure using src-folder I would be immensely grateful!

Right now I've just given up and using (which I guess technically is the preferred python way), just using "package-name" for folder instead and no src folder which obv works without issue. I would just much rather use the src style.

I'm not sure if this is the correct/best way to do it but at least it works for me to use environment variable PYTHONPATH:

provider:
  environment:
    PYTHONPATH: src

functions:
  someFunction:
    handler: my_package.some_handler.process

Unfortunately, this does not work anymore if you set layer: true because the requirements are inside the layer in the folder python and not in the lambda workdir.

I would be grateful for a better solution or a solution for the layer issue.

Hey, I am more or less struggling / thinking about the same right now. Currently, I ended up like this

my-repository-name
│── serverless.yml
│── README.md
│── src
  |── pyproject.toml
  |── poetry.lock
  │── my_package
    │── app.py
  │── tests

with

plugins:
  - serverless-python-requirements

custom:
  pythonRequirements:
    usePoetry: true

functions:
  functionName:
    module: src
    handler: my_package/app.handler_name
..

The only thing issues right now is that serverless-python-requirements does not recognize the poetry.lock file in src/ but expects it to be in the root folder. But this can be addressed by fixing the requirements.

Here are my package.json dependencies

  "dependencies": {
    "serverless": "^3.24.1",
    "serverless-python-requirements": "^6.0.0",
  }

Edit:

You also need to set package: individually: true in the serverless file.
See: https://medium.com/@nate_mitchell/serverless-packaging-user-defined-python-modules-41808776eae0