Skip to main content

Automatically modularize monolithic Python files into packages

Project description

Moedularizer

PyPI version CI License

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

  1. Analyze — Parse Python source via ast, extract symbols (classes, functions, constants) and their dependencies
  2. Cluster — Group symbols by dependency density, respecting configurable heuristics (separate dataclasses, constants, pure functions)
  3. Generate — Produce module files with proper from pkg.mod import sym cross-references
  4. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

moedularizer-0.2.0.tar.gz (170.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

moedularizer-0.2.0-py3-none-any.whl (65.6 kB view details)

Uploaded Python 3

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

Hashes for moedularizer-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e2684bc8abd6e90b6a90e04190bd5b3b820df26d1d2e6782c459857197008504
MD5 36c95172f757d3c901d610eb9bf2a94e
BLAKE2b-256 a98a8d52f6fe245d7ddfe3f4b5c5c15561dbb6b982e57cc494c47ff8fba8bced

See more details on using hashes here.

Provenance

The following attestation bundles were made for moedularizer-0.2.0.tar.gz:

Publisher: publish.yml on moeshawky/moedularizer

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for moedularizer-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e4809360c253207e580d4a12e2dd92102c4ba12f95d1778c32150d5d070ff584
MD5 b5348a24f84aa89c0619f6593ad11d98
BLAKE2b-256 601b7b2939b6ae3125578518e7789714f0159a292d16b44a3100bc642c8659ef

See more details on using hashes here.

Provenance

The following attestation bundles were made for moedularizer-0.2.0-py3-none-any.whl:

Publisher: publish.yml on moeshawky/moedularizer

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page