Black: Python Code Formatter Guide | Generated by AI

Home 2025.08

Black is an opinionated, uncompromising Python code formatter developed by the Python Software Foundation (PSF). It automatically reformats Python code to conform to a consistent style, primarily based on a strict subset of PEP 8, while emphasizing readability, consistency, and minimal diffs in version control. By using Black, developers agree to relinquish control over minor formatting details in exchange for speed, determinism, and reduced debates over style during code reviews. Black ensures that formatted (“blackened”) code looks uniform across projects, saving time and mental energy for more critical aspects of development. It supports Python 3.8 and later, with the latest stable release being version 25.1.0 (released on January 29, 2025), which introduces the 2025 stable style with enhancements like normalized Unicode escape casing and improved handling of trailing commas.

Black’s philosophy prioritizes:

It’s widely used in open-source and professional projects for its reliability and integration capabilities.

Installation

Black is available on PyPI and can be installed using pip. It’s recommended to install it in a virtual environment for project isolation.

On Arch Linux, you can install via the package manager: pacman -S python-black.

Black can also be installed via conda or other package managers. After installation, verify with black --version.

For development or testing, you can clone the GitHub repository and install in editable mode:

git clone https://github.com/psf/black.git
cd black
pip install -e .

Usage

Black is primarily a command-line tool. The basic command formats files or directories in place:

black {source_file_or_directory}

If running Black as a script doesn’t work (e.g., due to environment issues), use:

python -m black {source_file_or_directory}

Key Command-Line Options

Black offers various flags for customization and control. Here’s a summary of the main ones:

Examples

Tips and Notes

Configuration

Black can be configured via command-line flags or a pyproject.toml file (preferred for projects). Configuration in pyproject.toml goes under a [tool.black] section.

Using pyproject.toml

Example:

[tool.black]
line-length = 79
target-version = ['py311']
include = '\.pyi?$'
exclude = '''
/(
    \.git
  | \.hg
  | \.mypy_cache
  | \.tox
  | \.venv
  | _build
  | buck-out
  | build
  | dist
)/
'''
skip-string-normalization = true

Supported options mirror CLI flags (e.g., line-length, skip-string-normalization). Multi-value options like target-version use arrays.

Precedence

File Discovery and Ignoring

Black automatically discovers Python files in directories, respecting .gitignore by default. Use --include/--exclude to customize. It ignores common directories like .git, .venv, etc., unless overridden.

For version control, integrate with tools like pre-commit to enforce formatting.

The Black Code Style

Black enforces a specific style with limited configurability. Key rules:

Line Length

Strings

Numeric Literals

Line Breaks and Operators

Trailing Commas

Comments

Indentation

Empty Lines

Imports

Other Rules

Black aims to reduce diffs and improve readability, with changes mostly for bug fixes or new syntax support.

Integrations

Black integrates seamlessly with editors and version control for automated formatting.

Editors

Version Control

Advanced Topics

For troubleshooting, check the FAQ on the official docs or GitHub issues.

References


Back Donate