Tach - Python Modular Architecture Tool

0.34.1 · active · verified Sat Apr 11

Tach is a Python CLI tool (implemented in Rust) designed to enforce modular design and control dependencies between Python modules within a project. It enables developers to define explicit module boundaries, prevent deep coupling, enforce public interfaces, and detect circular dependencies. Actively maintained with frequent releases, the current version is 0.34.1, requiring Python 3.9 or higher.

Warnings

Install

Quickstart

To get started, first initialize a `tach.toml` file in your project root, which can be done interactively using `tach init` or by creating it manually. You define modules and their allowed dependencies (or layers) within this file. The `tach mod` command provides an interactive editor for defining module boundaries. After defining your boundaries, run `tach sync` to automatically populate dependency rules based on existing imports, then use `tach check` to validate your architecture. This example creates a basic project structure with a `tach.toml` to illustrate module `app` depending on `utils`.

import os
import textwrap

# Create a dummy project structure
os.makedirs("my_project/src/app", exist_ok=True)
os.makedirs("my_project/src/utils", exist_ok=True)

with open("my_project/src/app/__init__.py", "w") as f:
    f.write("")
with open("my_project/src/app/main.py", "w") as f:
    f.write("from src.utils.helpers import greet\n\ndef run():\n    print(greet('World'))\n")
with open("my_project/src/utils/__init__.py", "w") as f:
    f.write(""
)
with open("my_project/src/utils/helpers.py", "w") as f:
    f.write("def greet(name):\n    return f'Hello, {name}'\n")

tach_toml_content = textwrap.dedent("""
    [[layers]]
    name = "app"
    path = "src/app"
    dependencies = ["utils"]

    [[layers]]
    name = "utils"
    path = "src/utils"
    dependencies = []
""")

with open("my_project/tach.toml", "w") as f:
    f.write(tach_toml_content)

print("Project structure and tach.toml created in 'my_project' directory.")
print("Navigate to 'my_project' and run 'tach check' to validate boundaries.")

view raw JSON →