Black format exclude multiple directories

To exclude multiple directories from Black Python code formatter, use the following format in pyproject.toml. The multi-line regex format seems to be required–any other way didn’t take effect.

Edit / add / remove as many directories as desired, using the following multi-line format (indentation is not important). Note the escaping needed for “.” since this is a regex.

This is particularly useful when using Black in a project with Git submodules to not disturb the Git submodule Python code with Black from the top-level project. Likewise for other tools such as flake8 and mypy set exclude in their settings for Git submodules.

[tool.black]
force-exclude = '''
/(
\.git
| \.mypy_cache
| \.venv
| _build
| build
| dist
)/
'''