Skip to main content

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

  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.0.tar.gz (117.8 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.0-py3-none-any.whl (38.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: moedularizer-0.1.0.tar.gz
  • Upload date:
  • Size: 117.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.16 {"installer":{"name":"uv","version":"0.11.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for moedularizer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5711b898d35cfc61fbc5a4d81ef4ffd29dce4d2d4d29034e092a6bef1c0c7f21
MD5 68c3ea354b4bf3cdc006ab267135c1a3
BLAKE2b-256 77ac817ebfd9b9d7cca9fb81ec901ba0522e594957a723b9cf7a8a583566cb34

See more details on using hashes here.

File details

Details for the file moedularizer-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: moedularizer-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 38.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.16 {"installer":{"name":"uv","version":"0.11.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for moedularizer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1780c0d556aec56041499b2a25d73e71be13f24c40f87b3a567b9091a926cd93
MD5 cc5559ea300551a74876564e46bd301a
BLAKE2b-256 f21a4ce150b61cea6bf944cdabca58a4f1aac7785544c3b0490f08c6e0359c5a

See more details on using hashes here.

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