A guide to using ruff for Python code linting and formatting, including installation, usage, and integration with Positron.
This document provides a comprehensive guide to using ruff, a fast and efficient Python linter and formatter. It covers the essential steps for getting started with ruff, including installation, checking your code for issues, and automatically fixing them. The guide also demonstrates how to format your code with ruff and how to integrate it as an extension in the Positron IDE. Additionally, it outlines a practical workflow for using ruff on .qmd files by first converting them to .py format.
⚡️ 10-100x faster than existing linters (like Flake8) and formatters (like Black)
🐍 Installable via pip
🛠️ pyproject.toml support
install ruff
playing.py
#| eval: falsepip install ruff
check code
before correct python file
playing.py
import pandasfrom importlib.metadata import versionprint('test.py is running')print('version is :')print(version('rich'))a=3+1a
Code
Terminal
!ruff check playing.py
playing.py:6:8: F401 [*] `pandas` imported but unused
|
6 | import pandas
| ^^^^^^ F401
7 |
8 | from importlib.metadata import version
|
= help: Remove unused import: `pandas`
Found 1 error.
[*] 1 fixable with the `--fix` option.
fix code
Code
Terminal
!ruff check playing.py --fix
after fix
Code
Terminal
import pandasfrom importlib.metadata import versionprint('test.py is running')print('version is :')print(version('rich'))a=3+1a
format code
Code
Terminal
!ruff format playing.py
after format
Code
Terminal
import pandasfrom importlib.metadata import versionprint('test.py is running')print('version is :')print(version('rich'))a=3+1a
add ruff as extension in positron
using ruff on .qmd file
ruff only work on .py file, So if want to use ruff to check .qmd file then it need to convert to .py first.
---title: "Python code optimization with ruff"author: "Tony D"execute: warning: false error: falsedate: "2025-03-14"categories: - Tool - Pythonimage: "images.png"jupyter: python3---A guide to using `ruff` for Python code linting and formatting, including installation, usage, and integration with Positron.This document provides a comprehensive guide to using `ruff`, a fast and efficient Python linter and formatter. It covers the essential steps for getting started with `ruff`, including installation, checking your code for issues, and automatically fixing them. The guide also demonstrates how to format your code with `ruff` and how to integrate it as an extension in the Positron IDE. Additionally, it outlines a practical workflow for using `ruff` on `.qmd` files by first converting them to `.py` format.- ⚡️ 10-100x faster than existing linters (like Flake8) and formatters (like Black)- 🐍 Installable via `pip`- 🛠️ `pyproject.toml` support# install ruff``` {.python filename="playing.py"}#| eval: falsepip install ruff```# check codebefore correct python file``` {.python filename="playing.py"}import pandasfrom importlib.metadata import versionprint('test.py is running')print('version is :')print(version('rich'))a=3+1a``````{python}#| filename : Terminal!ruff check playing.py```# fix code```{python}#| filename : Terminal#| eval: false!ruff check playing.py --fix```after fix```{python}#| filename : Terminal#| eval: false{{< include playing.py >}}```# format code```{python}#| filename : Terminal#| eval: false!ruff format playing.py```after format```{python}#| filename : Terminal#| eval: false{{< include playing.py >}}```# add ruff as extension in positron# using ruff on .qmd fileruff only work on .py file, So if want to use ruff to check .qmd file then it need to convert to .py first.## convert .qmd to .py```{python}#| filename : Terminal#| eval: falsequarto convert index.qmd # → index.ipynb``````{python}#| filename : Terminal#| eval: false!jupyter nbconvert --to python index.ipynb # → index.py```## check .py with ruff```{python}#| filename : Terminal#| eval: false!ruff check index.py```# reference:https://github.com/astral-sh/ruff