Python code formatter libraries

Updated Nov 2026

Python code formatters rewrite source files in one consistent style. These libraries format code on save or in a pre-commit hook so that review diffs show changes in behavior rather than style.

Popularity

Ruff has the most installs and the most GitHub stars.

LibraryMonthly downloadsStarsLast releaseLicense
Ruff
428.0M
51.3KNov 2026MIT
Black
116.2M
42.2KOct 2026MIT
isort
98.7M
7.0KSep 2026MIT
autopep8
8.2M
4.7KJan 2025MIT License
YAPF
5.7M
14.0KNov 2024Apache Software License
Downloads: PyPI, last thirty days. Stars and releases: GitHub. Amber: no release in over a year.

Libraries and code examples

Ruff's formatter is a Python code formatter that runs as ruff format, a subcommand of the Ruff linter. The formatter is a drop-in replacement for Black. On large Black-formatted projects such as Django and Zulip, nearly every line comes out identical. Unlike Black, Ruff lets a project set the quote style, the indent style, and line endings. With docstring-code-format turned on, the formatter also reformats code examples inside docstrings. The --check flag lists unformatted files and exits with an error status, leaving the files unchanged.

Downloads / mo
428.0M
Stars
51.3K
Last release
0.17.5
License
MIT

Strengths

  • Formats docstring examples. Turn on docstring code formatting to format the Python examples inside docstrings.
  • Formats notebooks natively. Run ruff format on Jupyter notebooks without a separate wrapper.

Limitations

  • No import sorting. The formatter leaves import order alone. Sorting imports takes the linter's import rules first, with ruff check --select I --fix.
  • Not interchangeable with Black. Ruff differs from Black in a few deliberate ways. A project uses one of the two formatters for all its files.

Quickstart

From formatter.md
$ pip install ruff
ruff format                   # Format all files in the current directory.

Ruff examples

From open-source projects on GitHub

Black is a Python code formatter with almost no style settings. A project that adopts Black gives up control over formatting details. In return, every Black-formatted codebase looks alike. The black command takes files or directories and rewrites the code in place, keeping diffs as small as possible. Jupyter notebook support comes from installing the jupyter extra, as in pip install "black[jupyter]". Ruff follows Black's style closely as a drop-in replacement.

Downloads / mo
116.2M
Stars
42.2K
Last release
26.10.1
License
MIT

Strengths

  • Checks the code still matches. After formatting, Black confirms the AST is unchanged. The --fast flag skips this check.
  • Formats Jupyter notebooks. Pass --ipynb to format any input as a notebook, such as code piped on standard input.

Limitations

  • Few settings. Choices such as quote style and indent style are fixed. Ruff lets a project configure both.

Quickstart

From README.md
$ pip install black
black {source_file_or_directory}

Black examples

From open-source projects on GitHub

isort sorts Python imports alphabetically and separates them into sections for the standard library, third-party packages, and project modules. The isort command rewrites files in place. With --diff, the command prints the proposed changes instead of applying them. From Python, isort.code returns a copy of a source string with the imports sorted. A black profile makes isort wrap long imports the way Black does. With that profile set, the two tools agree on import layout.

Downloads / mo
98.7M
Stars
7.0K
Last release
9.0.2
License
MIT

Strengths

  • Sorts imports in a string. Call isort.code with source text to get it back with imports sorted.
  • Works alongside Black. Set the isort profile to black, and the two tools agree on import layout.

Limitations

  • Imports only. Black or Ruff formats the rest of each file.
  • Atomic mode off. The --atomic flag skips changes that would introduce syntax errors. The flag is off by default because it stops isort from running on code written for a different Python version.

Quickstart

From README.md
$ pip install isort
import isort

sorted_code = isort.code("import b\nimport a\n")

isort examples

From open-source projects on GitHub

autopep8 rewrites Python code to fix the style problems that pycodestyle, a Python style checker, reports when code breaks the official Python style guide. By default, the fixes change only whitespace. Each --aggressive flag allows further fixes, such as replacing x == None with x is None. From Python, the fix_code function takes a string of source and returns the fixed string. Unlike Black, which reformats every line in its own style, autopep8 changes only the lines pycodestyle flags.

Downloads / mo
8.2M
Stars
4.7K
Last release
2.3.2
License
MIT License

Strengths

  • Fixes selected rules. Pass --select or --ignore with pycodestyle codes to choose which fixes run.
  • Formats code strings. Call autopep8.fix_code() on a string of source code.

Limitations

  • Behavior-changing fixes. Changing x == None to x is None changes behavior when a class overrides __eq__. Fixes of that kind run only with --aggressive.
  • Some reports stay unfixed. Bad indentation that breaks syntax, and comments that look like code, are left for a person to fix.

Quickstart

From README.rst
$ pip install --upgrade autopep8
import autopep8
autopep8.fix_code('x= 123\n')

autopep8 examples

From open-source projects on GitHub

YAPF is a Python formatter derived from clang-format. For each piece of code, the algorithm computes the layout that best fits the configured style. That style starts from a predefined one, such as Google's, with individual settings in a .style.yapf file or pyproject.toml. Two calls make up the library API. FormatCode formats a string of code, while FormatFile reads and formats a file. Both accept a lines argument that limits formatting to chosen line ranges, the way editors format a selection. YAPF exposes dozens of style settings, compared with the handful in Black.

Downloads / mo
5.7M
Stars
14.0K
Last release
0.43.0
License
Apache Software License

Strengths

  • Many style settings. Tune individual formatting knobs; the style.py module lists every one.
  • Formats chosen lines. Pass a lines argument to format a snippet instead of the whole file, as editors do.

Limitations

  • Newer syntax missing. Type parameter syntax and the formalized f-string grammar from recent Python releases aren't supported yet.
  • Large data literals. Large data literals can come out badly formatted. The README suggests wrapping them in # yapf: disable and # yapf: enable comments.
  • Beta releases. The README describes YAPF as beta. Released versions may change often.

Quickstart

From README.md
$ pip install yapf
from yapf.yapflib.yapf_api import FormatCode  # reformat a string of code
formatted_code, changed = FormatCode("f ( a = 1, b = 2 )")

YAPF examples

From open-source projects on GitHub

FAQ

Use Ruff for a new project. It formats and lints in one tool, and its formatter is a drop-in replacement for Black. Keep Black in projects that already run it, and add isort with the Black profile to sort imports.

Both give nearly the same output, because Ruff's formatter is a drop-in replacement for Black, but it differs in a few deliberate ways, so pick one and don't alternate. Ruff also lints in the same tool, and its formatter doesn't sort imports, so run ruff check --select I --fix before ruff format.

Run ruff format to format every Python file in the current directory. In CI, add --check to fail on unformatted files without writing any changes.

Use Black to reformat whole files in one opinionated style. Use autopep8 to fix only the problems pycodestyle reports: by default it changes whitespace and leaves the rest of the layout as written.

Use Black for one fixed style with little to configure. Choose YAPF to match a custom house style through its many style settings, but its last release was in Nov 2024, and it doesn't support some newer Python syntax yet.

For coding agents

Found your code formatter library? Help your agent use it.

Connect Exa so your coding agent can find the library’s documentation, check its API, and look up implementations while you build.

Run in your terminal

claude plugin install exa@claude-plugins-official