Black Python formatter
Light · Postcard
102 × 152 mm · PDF
Dark · Postcard
102 × 152 mm · PDF
Ink saver · Postcard
102 × 152 mm · PDF
Light · A4 landscape
297 × 210 mm · PDF
Dark · A4 landscape
297 × 210 mm · PDF
Ink saver · A4 landscape
297 × 210 mm · PDF
Light · A3 landscape
420 × 297 mm · PDF
Dark · A3 landscape
420 × 297 mm · PDF
Ink saver · A3 landscape
420 × 297 mm · PDF
Start here
black .black <file.py>
black --check .black --diff .black -l <n> .
black -black --versionblack -v .Concepts
- Source files .py .pyi .ipynb
- Black config + CLI flags
- Equivalence check AST must match
- Formatted files exit 0 / 1 / 123
Run
black .black <file.py> <dir>
black --check .black --diff .black --check --diff .
black --diff --color .
black -q .black -v .black --versionblack -W <n> .
Style options
black -l <n> .
black -t <py311> .
black -S .black -C .black --pyi <file>
black --ipynb <file>
black -x <file>
black --preview .
black --unstable .
Files and stdin
black -black -c "<code>"
black --stdin-filename <path> -
black --extend-exclude '<regex>' .
black --exclude '<regex>' .
black --force-exclude '<regex>' .
black --include '<regex>' .
black --line-ranges <1-20> <file>
Config
[tool.black]black --config <file> .
black --required-version <26> .
black --no-cache .
black --cache-dir <dir> .
black --fast .BLACK_CACHE_DIR / BLACK_NUM_WORKERS
| Key | Same as |
|---|---|
line-length |
-l |
target-version |
-t (a list) |
skip-string-normalization |
-S |
extend-exclude |
--extend-exclude |
required-version |
--required-version |
Ignoring code
# fmt: skip# fmt: off# fmt: on# fmt: skip # noqa
Exit codes
| Code | Meaning |
|---|---|
0 |
Done, or nothing to change with --check |
1 |
--check: files would be reformatted |
123 |
Internal error |
Recipes
[tool.black]line-length = 100target-version = ['py311']extend-exclude = '''/(\.tox|migrations)/'''
black --check --diff .
git diff --name-only --diff-filter=d -- '*.py' | xargs black
# .pre-commit-config.yamlrepos: - repo: https://github.com/psf/black-pre-commit-mirror rev: 26.10.1 hooks: - id: black



