NorwegianClassic
NorwegianClassic

Reputation: 1115

Import self made Python module from outside serverless folder

I have a serverless folder with a Python AWS Lambda Function in it. I want to include a module from outside of the serverless folder. This is my folder structure:

projectRepo/
├── serverless/
│   ├── serverless.yml
│   ├── handler.py
├── src/
│   ├── main.py
└── └── utils/
      ├── __init__.py # from .myModule import *
      └── myModule.py

I have added sys.path.append(..\src\utils) in handler.py and this works locally. However, when triggering handler.py lambda in AWS I get: No module named 'utils'

Is there a way I can import myModule.py into handler.py and still leave it where it is now?

Upvotes: 3

Views: 1529

Answers (1)

pandrey
pandrey

Reputation: 364

Add (empty) __init__.py files in the serverless, src and projectRepo folders. Then, you can use the from projectRepo.src.utils import whatever_functions in handler.py, provided you are running your code with projectRepo as working directory (unless you actually add a package setup to install your code as a package, e.g. in a local virtual environment).

Upvotes: 3

Related Questions