Proper python directory structure for aws lambda deployment and vscode testing

I’m having a hard time figuring out the “best” layout for a lambda project. I know there are tons of similar python layout questions, however none of them quite solve my particular use case, though I don’t think what I’m trying to achieve is all that strange. The major requirements are:

  1. Deployable to lambda (I use terragrunt/terraform, specifically this module, though I don’t think it’s super relevant. It ultimately just zips up the directory you give it, and uploads it to lambda)
  2. A separate test directory that DOES NOT get uploaded to lamba (since there are strict size limitations with lambda, and it’s just good policy to keep things small)
  3. I can run the tests in vsCode (currently using pytest through the vsCode Testing tab).

My directory structure looks like this (simplified):
.
├── lambda.tf
├── main.tf
├── src
│ ├── init.py
│ ├── lambda_handler.py
│ ├── file1.py
│ ├── requirements.txt
│ ├── subdir
│ │ ├── init.py
│ │ ├── file2.py
│ │ ├── file3.py
├── test
│ ├── init.py
│ ├── test1.py
│ ├── requirements.txt
│ ├── subdir
│ │ ├── init.py
│ │ └── test2.py
│ │ └── test3.py

The src directory is the one that gets zipped up and uploaded to the lambda, and it is configured to run a method in lambda_handler.py. The test directory might have it’s own, distinct requirements.txt, and does not get uploaded to lambda.

lambda_handler.py has the following import statement:
from file1 import <method>

file1.py has the following import statements:
from subdir import file1
from subdir.file2 import <method>

test1.py has the following import statement:
from src import file1

test2.py has the following import statement:
`from src.subdir import file2

I also have the following setting.json for vscode. This gets linting working, and makes it possible to run tests.

{
  "jira-plugin.workingProject": "",
  "python.testing.pytestArgs": [
    "."
  ],
  "python.testing.unittestEnabled": false,
  "python.testing.pytestEnabled": true,
  "terminal.integrated.env.osx": {
    "PYTHONPATH": "${workspaceFolder}/modules/invoices/src:${workspaceFolder}/modules/invoices/test",
  },
  "terminal.integrated.env.linux": {
    "PYTHONPATH": "${workspaceFolder}/modules/invoices/src:${workspaceFolder}/modules/invoices/test",
  },
  "terminal.integrated.env.windows": {
    "PYTHONPATH": "${workspaceFolder}/modules/invoices/src:${workspaceFolder}/modules/invoices/test",
  },
  "python.envFile": "${workspaceFolder}/.env",
  "python.testing.cwd": "${workspaceFolder}/modules/invoices/test",
}

With the structure above, I can deploy to lambda and everything works as expected in the cloud. However, I get errors like the below when pytest via vscode does it’s test discovery (modified to match simplified dir structure above):

_______________ ERROR collecting test/test1.py ________________
ImportError while importing test module '<redacted>test/test1.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
../../../.pyenv/versions/3.11.5/lib/python3.11/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
test/test1.py:3: in <module>
    from src import file1
src/file1.py:1: in <module>
    from subdir.file2 import <method>
E   ModuleNotFoundError: No module named 'subdir'

I can get the test discovery (and then testing itself) to work if I change import statements to use relative paths
from .subdir.file2 import <method>
from ..subdir import file1
and the like, but then once I upload it, everything in the lambda fails with errors like the following:

Runtime.ImportModuleError: Unable to import module 'lambda_handler': attempted relative import with no known parent package

I haven’t figured out how to get both to work. I’m also not sure I need the init.py files; I added them because they get mentioned a lot in answers to similar questions, but they don’t seem to have made any difference. I’d like to remove them if they’re not helping the situation.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật