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.1.1.tar.gz (119.3 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.1.1-py3-none-any.whl (39.5 kB view details)

Uploaded Python 3

File details

Details for the file moedularizer-0.1.1.tar.gz.

File metadata

  • Download URL: moedularizer-0.1.1.tar.gz
  • Upload date:
  • Size: 119.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for moedularizer-0.1.1.tar.gz
Algorithm Hash digest
SHA256 43955ecdfbe3d656419de77fae8cf4d4ce1defe184bf4ff23dfa42441e34afc0
MD5 634861a63d606e17fc529ff3298857af
BLAKE2b-256 a7c47391a87afe854086b56e9ee9ec46d9f645d4d61d81a6175c77fe7014396b

See more details on using hashes here.

Provenance

The following attestation bundles were made for moedularizer-0.1.1.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.1.1-py3-none-any.whl.

File metadata

  • Download URL: moedularizer-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 39.5 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.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 79fbcff87c7a710b74f1f910862a2b072b1e4f05cec1b76dcf81bd13a5491753
MD5 d1d80da94f08cc7a3e109d319a3c97aa
BLAKE2b-256 d8d6e7d00f73c0171433ffeb116004c698bb189847ce44c8849062eb4a11010e

See more details on using hashes here.

Provenance

The following attestation bundles were made for moedularizer-0.1.1-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