Automatically modularize monolithic Python files into packages
Project description
Moedularizer
Automatically modularize monolithic Python files into packages.
Given a single Python file, moedularizer analyzes symbol dependencies, clusters related code, and generates a proper package structure with cross-module imports and a re-exporting __init__.py.
Install
pip install moedularizer
Zero dependencies beyond the Python standard library.
Quick Start
# Command line
python -m moedularizer.cli monolith.py ./my_package/ --package-name my_package
# Dry run (preview without writing)
python -m moedularizer.cli monolith.py ./my_package/ --package-name my_package --dry-run
# Verbose output
python -m moedularizer.cli monolith.py ./my_package/ --package-name my_package --verbose
# Programmatic API
from moedularizer import Moedularizer, MoedularizerConfig
from pathlib import Path
config = MoedularizerConfig(
source_file=Path("monolith.py"),
output_dir=Path("my_package/"),
package_name="my_package",
)
mod = Moedularizer(config)
result = mod.modularize(Path("monolith.py").read_text())
print(f"Generated {len(result.modules)} modules")
print(f"Preserved exports: {len(result.preserved_exports)}")
for warning in result.warnings:
print(f"Warning: {warning}")
if not result.errors:
mod.write(result)
How It Works
- Analyze — Parse Python source via
ast, extract symbols (classes, functions, constants) and their dependencies - Cluster — Group symbols by dependency density, respecting configurable heuristics (separate dataclasses, constants, pure functions)
- Generate — Produce module files with proper
from pkg.mod import symcross-references - Validate — Check for circular imports, API preservation, module naming, symbol coverage
Options
| Flag | Default | Description |
|---|---|---|
--max-symbols |
10 | Max symbols per generated module |
--separate-dataclasses |
True | Put dataclasses in their own module |
--separate-constants |
True | Put constants in their own module |
--absolute-imports |
True | Use absolute imports |
--dry-run |
False | Preview without writing files |
--no-backup |
False | Skip backing up existing files |
--verbose |
False | Detailed symbol/dependency/cluster output |
--force-groupings |
— | Force symbols together: name=Sym1,Sym2 |
License
MIT
Project details
Release history Release notifications | RSS feed
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 moedularizer-0.2.0.tar.gz.
File metadata
- Download URL: moedularizer-0.2.0.tar.gz
- Upload date:
- Size: 170.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e2684bc8abd6e90b6a90e04190bd5b3b820df26d1d2e6782c459857197008504
|
|
| MD5 |
36c95172f757d3c901d610eb9bf2a94e
|
|
| BLAKE2b-256 |
a98a8d52f6fe245d7ddfe3f4b5c5c15561dbb6b982e57cc494c47ff8fba8bced
|
Provenance
The following attestation bundles were made for moedularizer-0.2.0.tar.gz:
Publisher:
publish.yml on moeshawky/moedularizer
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
moedularizer-0.2.0.tar.gz -
Subject digest:
e2684bc8abd6e90b6a90e04190bd5b3b820df26d1d2e6782c459857197008504 - Sigstore transparency entry: 1825388916
- Sigstore integration time:
-
Permalink:
moeshawky/moedularizer@d72d50cc40812dc227da6ed1be73538c8d9288ab -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/moeshawky
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@d72d50cc40812dc227da6ed1be73538c8d9288ab -
Trigger Event:
push
-
Statement type:
File details
Details for the file moedularizer-0.2.0-py3-none-any.whl.
File metadata
- Download URL: moedularizer-0.2.0-py3-none-any.whl
- Upload date:
- Size: 65.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e4809360c253207e580d4a12e2dd92102c4ba12f95d1778c32150d5d070ff584
|
|
| MD5 |
b5348a24f84aa89c0619f6593ad11d98
|
|
| BLAKE2b-256 |
601b7b2939b6ae3125578518e7789714f0159a292d16b44a3100bc642c8659ef
|
Provenance
The following attestation bundles were made for moedularizer-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on moeshawky/moedularizer
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
moedularizer-0.2.0-py3-none-any.whl -
Subject digest:
e4809360c253207e580d4a12e2dd92102c4ba12f95d1778c32150d5d070ff584 - Sigstore transparency entry: 1825389029
- Sigstore integration time:
-
Permalink:
moeshawky/moedularizer@d72d50cc40812dc227da6ed1be73538c8d9288ab -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/moeshawky
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@d72d50cc40812dc227da6ed1be73538c8d9288ab -
Trigger Event:
push
-
Statement type: