Black / Ruff Format
Automatic formatters eliminate style debates by enforcing one deterministic output. Ruff format (Black-compatible) is the recommended default; Black remains widely used in existing projects.
Search across all documentation pages
Automatic formatters eliminate style debates by enforcing one deterministic output. Ruff format (Black-compatible) is the recommended default; Black remains widely used in existing projects.
# Ruff format (recommended)
uv run ruff format .
# Black (legacy projects)
uv run black .When to reach for this:
# Before formatting
def calculate_total(items, tax_rate=0.1, discount=0):
subtotal=sum(i.price*i.qty for i in items)
return subtotal*(1+tax_rate)-discount
# After: uv run ruff format file.py
def calculate_total(items, tax_rate=0.1, discount=0):
subtotal = sum(i.price * i.qty for i in items)
return subtotal * (1 + tax_rate) - discount[tool.ruff.format]
line-length = 88
quote-style = "double"
indent-style = "space"uv run ruff format --check . # CI: fail if unformatted
uv run ruff format . # fix locallyWhat this demonstrates:
--check mode for CI without modifying filespyproject.toml applies project-wide| Aspect | Black | Ruff Format |
|---|---|---|
| Speed | Moderate | Very fast |
| Config | [tool.black] | [tool.ruff.format] |
| Compatibility | Reference impl | Black-compatible |
| Integration | Separate tool | Same binary as linter |
migrations/, generated/ in config.ruff format --check in CI.# fmt: off/on blocks should be rare. Fix: use sparingly for legitimate cases (long SQL strings).line-length.| Alternative | Use When | Don't Use When |
|---|---|---|
| autopep8 | Minimal changes to existing style | You want full determinism |
| yapf | Google style preference | Team wants Black-compatible |
| Manual formatting | Solo scripts | Any team project |
New projects: ruff format (faster, integrated). Existing Black projects: migrate when convenient.
88 (Black default) is the Python community standard. Match your linter config.
# fmt: off before and # fmt: on after the block.
Yes, by default to double quotes. Configure with quote-style = "single".
ruff format notebook.ipynb (ruff 0.6+). Black also supports notebooks.
No. Formatters only change whitespace and quote style, never semantics.
Set "editor.defaultFormatter": "charliermarsh.ruff" and "editor.formatOnSave": true.
Exclude them. Add exclude = ["migrations"] to avoid noisy diffs.
ruff format path/to/file.py or black path/to/file.py.
No. Import sorting is ruff check --fix (isort rules), not the formatter.
Stack versions: This page was written for Python 3.14.0, FastAPI 0.115+, Django 5.2, Flask 3.1, Pydantic 2, PyTorch 2.6+, pandas 2.2+, Polars 1.x, ruff 0.9+, and uv 0.6+.
Reviewed by Chris St. John·Last updated Jul 19, 2026