Skip to main content
Writing clean, consistently formatted code is important — not just for looks, but because messy code is harder to debug and share. Ruff is a modern Python tool that handles formatting, linting, and import sorting automatically, so you never have to think about style rules again.

What is Ruff?

Ruff is a modern Python tool that is incredibly fast and combines multiple tools in one:
  • Linting — finding issues and potential errors in your code
  • Formatting — automatically fixing code style
  • Import sorting — organizing imports cleanly
It is beginner-friendly with clear error messages and works seamlessly with VS Code.
A linter checks your code for style issues and potential errors. A formatter automatically fixes those issues for you. Ruff does both.

Install Ruff in VS Code

  1. Open VS Code
  2. Press Ctrl + Shift + X (Windows/Linux) or Cmd + Shift + X (macOS) to open Extensions
  3. Search for Ruff
  4. Install the official Ruff extension by Astral
The Ruff extension replaces older tools like Pylint, Black, and isort. You only need this one extension for all three jobs.

Enable format on save

Let VS Code automatically format your code every time you press Save:
  1. Open Settings with Ctrl + , (Windows/Linux) or Cmd + , (macOS)
  2. Search for format on save
  3. Check the box for Editor: Format On Save
  4. Search for default formatter
  5. Set Python → Formatting: Provider to Ruff

See Ruff in action

Here is messy code before saving:
After saving with Ruff enabled, the file is automatically reformatted to:
Ruff automatically:
  • Separated imports from the rest of the code
  • Added proper spacing around operators
  • Fixed indentation
  • Made quote style consistent (double quotes)
  • Reformatted the long list for readability
If formatting does not trigger on save, right-click inside your Python file and select Format Document. This runs Ruff manually and also confirms the extension is working.

Common formatting rules

Ruff follows Python’s official style guide (PEP 8) automatically:
  • 4 spaces for indentation (never tabs)
  • Spaces around operators (x = 1, not x=1)
  • Two blank lines between top-level functions
  • Maximum line length of 88 characters
You do not need to memorize any of these — Ruff handles them every time you save.

Understanding linting errors

Ruff will underline code issues in your editor. For example:
Hover over any underlined code to see:
  • What the issue is
  • Why it matters
  • How to fix it
Do not ignore linting warnings. They help you write better code and catch real bugs before they cause problems at runtime.

What’s next?

With Ruff set up, your code will always look professional and follow best practices automatically. Every file you save is clean, readable, and consistent.