black

From XennisWiki
Jump to: navigation, search

black is a strict code formatter for Python.

Installation

Use pip to install it

pip install black

Usage

CLI

Example usage

black --line-length 132 *.py some_dir/

Configuration file

black --config black.ini *.py

The configuration file black.ini

[tool.black]
line-length=132

Library

import black

filename = "example.py"

mode = black.FileMode()
mode.target_versions = [black.TargetVersion.PY38]
mode.line_length = 132
black.reformat_one(pathlib.Path(filename), fast=False, write_back=black.WriteBack.YES, mode=mode, report=black.Report(quiet=True)

See also

External links