DEV Community

MrRobot
MrRobot

Posted on

Black – The Uncompromising Python Code Formatter

Black is an opinionated Python code formatter that automatically reformats your code to follow a consistent style. It eliminates the need for manual formatting decisions, saving time and reducing stylistic debates in teams. It ensures clean, readable, and standardized code across projects.


Installation:

pip install black
Enter fullscreen mode Exit fullscreen mode

Example usage:

# Format a single file
black app.py

# Format all Python files in the current directory
black .

# Check files without changing them
black --check .

# Exclude certain files or folders
black . --exclude '/migrations/'
Enter fullscreen mode Exit fullscreen mode

PyPI page: https://pypi.org/project/black/
GitHub page: https://github.com/psf/black


3 Project Ideas:

  1. Add Black to a pre-commit hook to auto-format code before each commit.
  2. Combine Black with Flake8 for both formatting and linting in your projects.
  3. Use Black in CI/CD pipelines to enforce consistent style across team submissions.

Top comments (0)