Build focused Python Lambda pack bundles from source imports.
Project description
monopack
monopack builds per-pack Python Lambda bundles from a monolith-style repo.
Background
Go works well for multi-Lambda projects because one codebase can expose multiple cmd entrypoints and build each pack with only what it needs.
Python workflows are usually less ergonomic at that scale:
- AWS SAM and Serverless framework commonly package one Python artifact per project.
- Per-pack folder layouts can make shared code awkward.
- Workarounds such as local packages, symlinks, or copied shared folders often add maintenance overhead.
monopack is intended to make Python feel closer to that Go workflow without changing your project into N separate services. It takes a larger codebase and produces per-pack zip artifacts by:
- tracing imports from each pack entrypoint,
- copying reachable first-party files,
- deriving a minimal pinned
requirements.txtsubset for third-party imports, - installing only that dependency subset into the build target,
- and running optional verification/tests to increase confidence that each split artifact works in isolation.
It is intentionally conservative: this is import-based trimming with guardrails, not full tree-shaking or whole-program optimization. It aims to cover common Python import patterns used in real projects, while keeping behavior explicit and testable.
What monopack does
- Builds one or many packs from
packs/*.pyintobuild/<pack_name>/. - In
deploymode, writes deploy zip artifacts atbuild/<pack_name>.zip. - In
deploymode, writes package digest helper file(s) (build/<pack_name>.package.sha256by default). - In
testmode, copies relevant tests and runs them in the build target (no zip output). - Uses pinned project
requirements.txt(name==versionlines only). - Supports optional auto-fix for missing-module verification failures (
--auto-fix, opt-in).
Quickstart
Project expectations:
packs/directory with pack entrypoint files (<name>.py).- Project-level
requirements.txtcontaining pinnedname==versionlines. - Optional
tests/directory when using--mode test.
Build one pack in deploy mode:
PYTHONPATH=src python -m monopack users_get \
--packs-dir packs \
--build-dir build
Run confidence build in test mode:
PYTHONPATH=src python -m monopack users_get \
--packs-dir packs \
--build-dir /tmp/monopack-build \
--mode test
Build all packs discovered in packs/*.py (no target argument):
PYTHONPATH=src python -m monopack \
--packs-dir packs \
--build-dir build
CLI usage
Basic form:
PYTHONPATH=src python -m monopack [pack_name] [options]
Key flags for real-project usage:
--version: prints the installedmonopackversion and exits.--mode deploy|test: deploy builds runtime payload + zip; test builds payload + tests (no zip).--with-tests: deploy mode only; runs relevant tests before finalizing deploy payload.--verify/--no-verify: verifier is on by default.--auto-fix: opt-in auto-repair loop for missing imports during verify.--debug: emit aggregated import/dependency resolution report to stderr.--jobs: parallel workers for multi-pack builds (autodefault).--sha-output: comma-separated package digest outputs for deploy mode (hex,b64; defaulthex).
Package digest output guidance:
hex(.package.sha256): general CI/script diffing and human-readable checks.b64(.package.sha256.b64): Terraform-style workflows that prefer base64 digest values.- Use both when needed:
--sha-output hex,b64.
For full argument behavior and validation details, see docs/cli.md.
Constraints and limits
- Function discovery is shallow: only
packs/*.py(excluding names starting with_). - Pack names must use letters, numbers, and underscores; pack names cannot include
/,\\, or.. - Build directory must differ from packs directory and cannot be nested inside it.
- Requirements parser accepts only pinned
name==versionlines (comments and blanks allowed). - First-party graph traversal follows imports that resolve to local modules under the same project root as
packs/(excludingtests/). - Auto-fix only handles
ModuleNotFoundErrorflows and retries up to 3 times when enabled.
Recommended feedback loop for monolith split confidence
- Build the target in test mode with verification enabled:
PYTHONPATH=src python -m monopack <pack> --mode test --verify. - Inspect build output (
build/<pack>/) and generatedrequirements.txtfor expected scope. - Build deploy artifact once confidence is good:
PYTHONPATH=src python -m monopack <pack> --mode deploy. - Optionally gate deploy build with tests:
PYTHONPATH=src python -m monopack <pack> --mode deploy --with-tests. - Run repository tests to catch broader regressions:
python -m unittest discover -s tests -v. - Tighten imports/tests/inline config and repeat until scoped build behavior is stable.
Deeper docs
- CLI reference:
docs/cli.md - PyPI publishing:
docs/publishing.md - Fixture-driven confidence loop and matrix:
docs/testing-fixtures.md
For Terraform source_code_hash usage with generated package digests, see docs/cli.md.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file monopack-0.3.0.tar.gz.
File metadata
- Download URL: monopack-0.3.0.tar.gz
- Upload date:
- Size: 38.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e1ef83e6548c15bff908ed26852d4edb73e069a0daecdd4b2c58e3ca53ca6e59
|
|
| MD5 |
c32cb4c83cdee3927ae6507f639fd14a
|
|
| BLAKE2b-256 |
96a161936653f63eebabf8b2c0e5e385d22508742a82661a8eeb0085437a5f5e
|
File details
Details for the file monopack-0.3.0-py3-none-any.whl.
File metadata
- Download URL: monopack-0.3.0-py3-none-any.whl
- Upload date:
- Size: 24.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aaf18be2db51185743b17c729772d74f6542d3ea2680c56eb910b7f8e9ea3bdb
|
|
| MD5 |
12c1583b77ddf4a9f2f61b149b571e48
|
|
| BLAKE2b-256 |
44f50822172c1c869dbcb24a5acc65338f84d7dd92766b256c26651a3f657cb5
|