In IntelliJ IDEA with the Python plugin, or in PyCharm, choose Code | Reformat Code (Ctrl+Alt+L on Windows and Linux, Cmd+Option+L on macOS) to format a file with the IDE’s Python code style. On a team, run Black or Ruff from the IDE instead, so the IDE and CI produce the same output.
Table of Contents
Step 1: Reformat a Python file or selection in IntelliJ IDEA
IntelliJ IDEA formats Python through the Python plugin, which JetBrains’ Python code style page names as “Python (IntelliJ IDEA Ultimate)”; check it is enabled under Settings | Plugins. For IntelliJ IDEA, JetBrains’ Marketplace lists Python (plugin ID 631, Pythonid) and Python Community Edition (plugin ID 7322, PythonCore), whose pages say their feature sets correspond to PyCharm IDE with a Pro subscription and PyCharm IDE Community Edition respectively, so install the one you want. PyCharm has Python built in. Steps per JetBrains’ help for IntelliJ IDEA and PyCharm.
- Open the file. To format only part of it, select that code first; with nothing selected, the IDE reformats the whole file.
- Choose Code | Reformat Code or press its shortcut from the table below.
- For options, open the Reformat File dialog (shortcut below) or right-click the file in the Project tool window and choose Reformat Code. The dialog includes Optimize imports and Do not keep line breaks; click Run.
| Action | Windows and Linux | macOS |
|---|---|---|
| Reformat Code (selection or whole file) | Ctrl+Alt+L | Cmd+Option+L |
| Reformat File dialog | Ctrl+Alt+Shift+L | Cmd+Option+Shift+L |
| Open Settings | Ctrl+Alt+S | Cmd+, |
On Linux with the KDE keymap, Reformat Code is Alt+Shift+L.
Step 2: Set the Python right margin and wrapping rules
Open Settings, go to Editor | Code Style | Python and open Wrapping and Braces.
- Right margin. JetBrains’ Python page calls it Right margin; its general page calls it Hard wrap at. Use 88 for Black and Ruff’s default, or 79 for PEP 8’s default. PEP 8 lets a team that agrees raise it to 99.
- Ensure right margin is not exceeded. Select it so the formatter tries to keep lines inside the margin; JetBrains says it overrides the Do not wrap style.
- Keep when reformatting | Line breaks is on by default, so Reformat Code keeps the breaks you typed, while Black rejoins lines that fit unless a trailing comma holds them apart. Clear it to let the IDE re-wrap.
To share it, use the Project scheme, stored in .idea/codeStyles and shared through version control, per JetBrains.
Step 3: Reformat Python code on save in IntelliJ IDEA
Open Settings | Tools | Actions on Save and select Reformat code. Configure scope… excludes files and folders, the file types list limits what is reformatted, and Whole file or Changed lines applies under version control. Optimize imports is a separate checkbox. JetBrains’ Save and revert changes page says they run on every save, autosave included.
Step 4: Run Black in PyCharm and IntelliJ IDEA
PyCharm has had a built-in Black integration since 2023.2, per JetBrains’ What’s New page and Black’s editor documentation. IntelliJ IDEA’s Python language support page says its Python plugin implements all of PyCharm’s features and links to PyCharm’s Python tools support page. Install Black in the project’s interpreter and, per JetBrains, PyCharm offers to enable it. Where you switch it on depends on the release:
| PyCharm release | Black | Ruff |
|---|---|---|
| 2026.2 | Settings | Python | Tools | External Tools, then Configure | Same page |
| 2025.3 and 2026.1 | Settings | Python | Tools | Black | Settings | Python | Tools | Ruff |
| 2023.2 to 2025.2 | Settings | Tools | Black | Formatting added in 2025.3; before that, an External Tool |
The table gives PyCharm’s paths. JetBrains’ help puts Python Integrated Tools under Settings | Tools in IntelliJ IDEA but Integrated Tools under Settings | Python | Tools in PyCharm, so I could not confirm where IntelliJ IDEA lists Black and Ruff; search the Settings dialog for them.
From 2023.2 to 2026.1, JetBrains says On code reformat makes Black replace the built-in formatter for Ctrl+Alt+L, and On save runs it when you save. The 2026.2 page swaps those checkboxes for a per-tool Features widget and does not say which actions run the tool; after enabling Black, reformat a messy file and check it with black --check (exit 0 means nothing would change).
Step 5: Run Ruff in PyCharm and IntelliJ IDEA
JetBrains’ Python tools support page lists Ruff with Inspections, Formatting and Import optimizer options. The PyCharm 2025.3 release notes say you can now format your code with Ruff directly in PyCharm, and Astral’s setup guide says PyCharm supports Ruff out of the box from 2025.3. Install Ruff in the project’s interpreter, switch it on as in the table above and keep Formatting selected. Do not run Ruff and Black as formatters together: JetBrains says to disable overlapping formatting in Ruff when you use Black.
Step 6: Keep the IDE and formatter from fighting
PEP 8 says “Limit all lines to a maximum of 79 characters.” Black’s default is 88, and so is Ruff’s. With the IDE at 79 and the formatter at 88, each tool can rewrite the other’s output. Here is Black alone: an 87-character line passes black --check at the default, is wrapped by --line-length 79, then fails the default check.
$ cat invoice.py
def invoice_total(customer, line_items, discount_code):
return build_invoice_summary(customer, line_items, discount_code, include_tax=True)
$ black --check -q invoice.py; echo "exit: $?"
exit: 0
$ black -q --line-length 79 invoice.py
$ cat invoice.py
def invoice_total(customer, line_items, discount_code):
return build_invoice_summary(
customer, line_items, discount_code, include_tax=True
)
$ black --check -q invoice.py; echo "exit: $?"
exit: 1
Black at 79 stands in for code wrapped at 79 columns; I did not run the IDE’s formatter, so its behavior at that margin is untested. Set one limit, not two: make the IDE’s right margin 88, or let the formatter own the layout (JetBrains’ 2025.3 and 2026.1 pages say Reformat code then uses Black, with On code reformat selected, or Ruff). Afterwards, black --check or ruff format --check should exit 0.
Make it reproducible: commit a formatter config
An IDE setting only binds people who use that IDE. JetBrains says Black’s settings can live in pyproject.toml and Ruff’s in pyproject.toml, ruff.toml or .ruff.toml, so the IDE and CI can read one file. Pick one formatter, install it in the project’s virtual environment and commit its configuration:
pip install ruff
ruff format . # or: pip install black && black .
Black, Ruff and autopep8 on one file
I ran Black, Ruff and autopep8 on one messy file. The input:
import os,sys
def f( x,y = 3 ):
d = {'a':1, 'b':2}
if x>y : return [ i*2 for i in range(10) if i%2==0 ]
result = some_function_with_a_long_name(argument_one, argument_two, argument_three, argument_four)
return result
black and ruff format produced byte-identical output, and when I compared the two files with diff, it printed nothing and exited 0:
import os, sys
def f(x, y=3):
d = {"a": 1, "b": 2}
if x > y:
return [i * 2 for i in range(10) if i % 2 == 0]
result = some_function_with_a_long_name(
argument_one, argument_two, argument_three, argument_four
)
return result
Ruff’s documentation says its formatter “is designed as a drop-in replacement for Black” and that on extensive Black-formatted projects like Django and Zulip, “> 99.9% of lines are formatted identically.”
autopep8 is a different kind of tool and its output shows it:
import os
import sys
def f(x, y=3):
d = {'a': 1, 'b': 2}
if x > y:
return [i*2 for i in range(10) if i % 2 == 0]
result = some_function_with_a_long_name(
argument_one, argument_two, argument_three, argument_four)
return result
It kept single quotes, i*2 and the double space in the dict: pycodestyle has no quote check, and pycodestyle and autopep8 both skip E226 and E241 by default, so pycodestyle reports nothing on this output. It split import os, sys, which Black and Ruff left alone, because that is a pycodestyle error (E401). autopep8 fixes what pycodestyle flags; Black and Ruff rewrite the whole file to one style.
Black has almost no style options
Black’s README says: “the uncompromising Python code formatter. By using it, you agree to cede control over minutiae of hand-formatting.” The main options in black --help that change the style are -l/--line-length, -t/--target-version, -S/--skip-string-normalization, -C/--skip-magic-trailing-comma and the --preview and --unstable switches. There is no quote-style or indent-width option.
Ask for more and Black says so, then ignores it. A pyproject.toml containing:
[tool.black]
quote-style = "single"
indent-width = 2
made Black print this line (path shortened here), then format the file with its defaults and exit 0:
Invalid config keys detected: 'quote_style', 'indent_width' (in .../pyproject.toml)
Ruff does accept style options: [format] quote-style = "single" in ruff.toml made ruff format emit d = {'a': 1}, and a top-level indent-width = 2 gave two-space indents. So take Black’s style as given, or Ruff if you need a few knobs.
Enforce the config outside the editor
What does not move is the repository configuration, so put the authority there:
# pyproject.toml
[tool.ruff]
line-length = 88
[tool.ruff.format]
quote-style = "double"
Then enforce it outside the editor: a pre-commit hook or a CI step running ruff format --check . (or black --check .) catches everyone not using your IDE. --check writes nothing and exits non-zero when a file would change. With the sample saved as messy.py:
$ ruff format --check --output-format concise messy.py; echo "exit: $?"
messy.py:1:11: unformatted: File would be reformatted
1 file would be reformatted
exit: 1
$ ruff format -q messy.py
$ ruff format --check --output-format concise messy.py; echo "exit: $?"
1 file already formatted
exit: 0
I put the same-file runs in a script you can run; it installs the tools into a throwaway virtualenv and never opens an IDE. The same split works in VS Code, where a committed profile decides the output; I covered the Java setup in Format Java in VS Code.
What I tested and what comes from JetBrains’ docs
I ran Black 26.10.0, Ruff 0.16.10 and autopep8 2.3.2 (pycodestyle 2.15.0) on Python 3.11.16 in a throwaway virtual environment on macOS 26.6 (Apple M3 Max) on 6 October 2026, and I did not run IntelliJ IDEA or PyCharm, so every IDE step comes from JetBrains’ help pages and release notes, read the same day.
Not verified: which action runs Black or Ruff in the 2026.2 help, where IntelliJ IDEA lists Black and Ruff in Settings, and what the IDE’s own formatter does to the sample.