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
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
- Open VS Code
- Press
Ctrl + Shift + X(Windows/Linux) orCmd + Shift + X(macOS) to open Extensions - Search for Ruff
- 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:- Open Settings with
Ctrl + ,(Windows/Linux) orCmd + ,(macOS) - Search for format on save
- Check the box for Editor: Format On Save
- Search for default formatter
- Set Python → Formatting: Provider to Ruff
See Ruff in action
Here is messy code before saving:- 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
Common formatting rules
Ruff follows Python’s official style guide (PEP 8) automatically:- 4 spaces for indentation (never tabs)
- Spaces around operators (
x = 1, notx=1) - Two blank lines between top-level functions
- Maximum line length of 88 characters
Understanding linting errors
Ruff will underline code issues in your editor. For example:- What the issue is
- Why it matters
- How to fix it