Black

Black is an opinionated code formatter for Python that formats code to a consistent style with minimal configuration.

Installation and Setup

Install from the Python Package Index (PyPI) inside a virtual environment:

Windows PowerShell
PS> py -m pip install black
PS> py -m black --version  # Verify the installation
Shell
$ python -m pip install black
$ python -m black --version  # Verify the installation

Configuration typically lives in pyproject.toml at the project root:

TOML
[tool.black]
line-length = 88
target-version = ["py313"]
include = "\\.pyi?$"
exclude = [
  "^build/",
  "^dist/",
  "^.venv/"
]

Key Features

  • Formats code deterministically to reduce diffs and style debates.
  • Favors a single, consistent style and exposes only a few toggles like line length and target versions.
  • Reads configuration from pyproject.toml and respects include and exclude patterns.
  • Supports parallel and incremental formatting for speed on large projects.
  • Integrates with editors, pre-commit hooks, and continuous integration (CI) through standard exit codes.

Usage

Format a file or directory in place:

Shell
$ python -m black path/to/file.py
$ python -m black src/ tests/

Check formatting without writing changes:

Shell
$ python -m black --check .

Show a unified diff of proposed changes:

Shell
$ python -m black --diff path/to/file.py

Override the configured line length for a one-off run:

Shell
$ python -m black --line-length 100 .

Select a target Python version to guide formatting decisions:

Shell
$ python -m black --target-version py311 .

By Leodanis Pozo Ramos • Updated Dec. 1, 2025