Python

Lint and format Python with Ruff

Install Ruff in your project, find likely mistakes, fix the easy ones and format every file in one consistent style.

Fill these in once
  1. Go to your project folder
    Terminal
    cd my-project

    Use your own folder name.

  2. Create a virtual environment
    Terminal
    python3 -m venv .venv

    Skip this if your project already has one.

  3. Switch it on
    Terminal
    source .venv/bin/activate

    Your prompt now starts with (.venv).

  4. Install Ruff
    Terminal
    pip install ruff
  5. Check for problems
    Terminal
    ruff check .

    Lists unused imports, undefined names and other likely mistakes, with the file and line of each.

  6. Fix what Ruff can fix by itself
    Terminal
    ruff check --fix .

    Removes unused imports and similar easy fixes. Anything it can't fix stays in the list.

  7. Format every file
    Terminal
    ruff format .

    Rewrites spacing, quotes and line breaks in one consistent style, like Black.

  8. See the formatting changes without saving them
    Terminal
    ruff format --diff app.py

Check it worked

  1. Check the whole project is clean
    Terminal
    ruff check . && ruff format --check .

    Prints All checks passed! and says how many files are already formatted.

Undo it
  1. Put the files back as they were
    Terminal
    git restore .

    Only works if the project is in Git. It throws away every change since your last commit, not just Ruff's, so commit your own work first.

  2. Remove Ruff
    Terminal
    pip uninstall -y ruff
  1. Go to your project folder
    Terminal · Ubuntu / Debian
    cd my-project

    Use your own folder name.

  2. Create a virtual environment
    Terminal · Ubuntu / Debian
    python3 -m venv .venv

    Skip this if your project already has one. On Debian and Ubuntu you may need sudo apt install python3-venv first.

  3. Switch it on
    Terminal · Ubuntu / Debian
    source .venv/bin/activate

    Your prompt now starts with (.venv).

  4. Install Ruff
    Terminal · Ubuntu / Debian
    pip install ruff
  5. Check for problems
    Terminal · Ubuntu / Debian
    ruff check .

    Lists unused imports, undefined names and other likely mistakes, with the file and line of each.

  6. Fix what Ruff can fix by itself
    Terminal · Ubuntu / Debian
    ruff check --fix .

    Removes unused imports and similar easy fixes. Anything it can't fix stays in the list.

  7. Format every file
    Terminal · Ubuntu / Debian
    ruff format .

    Rewrites spacing, quotes and line breaks in one consistent style, like Black.

  8. See the formatting changes without saving them
    Terminal · Ubuntu / Debian
    ruff format --diff app.py

Check it worked

  1. Check the whole project is clean
    Terminal · Ubuntu / Debian
    ruff check . && ruff format --check .

    Prints All checks passed! and says how many files are already formatted.

Undo it
  1. Put the files back as they were
    Terminal · Ubuntu / Debian
    git restore .

    Only works if the project is in Git. It throws away every change since your last commit, not just Ruff's, so commit your own work first.

  2. Remove Ruff
    Terminal · Ubuntu / Debian
    pip uninstall -y ruff
  1. Go to your project folder
    PowerShell
    cd my-project

    Use your own folder name.

  2. Create a virtual environment
    PowerShell
    py -m venv .venv

    Skip this if your project already has one.

  3. Switch it on
    PowerShell
    .venv\Scripts\Activate.ps1

    Your prompt now starts with (.venv).

  4. Install Ruff
    PowerShell
    pip install ruff
  5. Check for problems
    PowerShell
    ruff check .

    Lists unused imports, undefined names and other likely mistakes, with the file and line of each.

  6. Fix what Ruff can fix by itself
    PowerShell
    ruff check --fix .

    Removes unused imports and similar easy fixes. Anything it can't fix stays in the list.

  7. Format every file
    PowerShell
    ruff format .

    Rewrites spacing, quotes and line breaks in one consistent style, like Black.

  8. See the formatting changes without saving them
    PowerShell
    ruff format --diff app.py

Check it worked

  1. Check the whole project is clean
    PowerShell
    ruff check .; ruff format --check .

    Prints All checks passed! and says how many files are already formatted.

Undo it
  1. Put the files back as they were
    PowerShell
    git restore .

    Only works if the project is in Git. It throws away every change since your last commit, not just Ruff's, so commit your own work first.

  2. Remove Ruff
    PowerShell
    pip uninstall -y ruff