Python Formatting with Ruff: Consistent Code, CI Checks, and Migrating from Black
Format Python consistently with Ruff, check formatting in CI without writing files, and understand VS Code setup and Black migration differences.
To keep indentation and line breaks consistent in a Python team, use Ruff's formatter and add a non-writing check to CI. A project already using Black can consider migrating, but the two tools do not always produce identical output. This is a documentation-based guide for developers starting with Ruff.
AI-generated illustration of irregular code lines being arranged into a consistent layout.
Documentation checked: November 30, 2026. The commands below are illustrative; Ruff was not installed or run in this environment.
Formatting and linting are different jobs
A formatter arranges presentation such as whitespace, indentation, and line breaks. A linter checks selected rules, including issues such as unused variables. Ruff provides both, but ruff format formats and ruff check lints. Passing a formatting check does not guarantee correctness or security. Ruff formatter documentation
Start with a small file
Activate the project's virtual environment and install Ruff. In a team, pin a version you have actually validated in a dependency or lock file so development and CI use the same version.
For example, create demo.py:
Inspect the proposed change before applying it:
--diff shows proposed changes without writing the file. ruff format modifies it in place. --check leaves files untouched and returns a non-success exit code if formatting is needed. To format a directory, use ruff format ., but review the diff before applying it to an existing repository. Commands and exit codes
Align project settings and CI
The following can go in the root pyproject.toml. If the sections already exist, merge the settings rather than creating duplicate sections.
These values match the documented defaults. Ruff supports pyproject.toml, ruff.toml, and .ruff.toml, and the closest configuration can apply to each file. Check the scope if the repository has configuration in subdirectories. Configuration
In CI, install the same Ruff version used by developers, then run:
The first command checks formatting; the second checks enabled lint rules. Neither applies fixes. Lint fixes require a separate ruff check --fix; review their scope before running it.
VS Code and migration from Black
In VS Code, install the Ruff extension and select it as the default formatter for Python files. Align the team’s project settings and formatter versions before enabling format on save. The executable used by the extension can differ from the version in a terminal or CI, which can change the output. VS Code Python formatting · Ruff editor setup
Ruff targets output similar to Black but has intentional differences, including how it handles trailing comments. When migrating an existing Black project, inspect the full diff on a separate branch and run tests, then standardize the editor and CI on one formatter. Alternating between both can repeatedly change some code. Differences from Black
Line length is a target, not a guarantee that every line will fit. Formatting checks do not replace tests or type checking. The examples in this article make no claim of execution against a particular Ruff version or production repository.