[
    {
        "role": "user",
        "content": "in%20ruff%20python%20extension%20for%20vs%20code%2C%20how%20to%20invoke%20import%20sorting"
    },
    {
        "role": "assistant",
        "content": "Here\u2019s how to invoke import sorting with the Ruff Python extension in VS Code, and how the whole import-sorting setup works:\n\n---\n\n## How import sorting works in Ruff\n\n- Ruff\u2019s formatter (`ruff format`) does *not* sort or reorganize imports. That\u2019s handled by Ruff\u2019s **linting rules** \u2014 specifically, rules with an `I` identifier (like `I001`) which detect unsorted or improperly grouped imports. ([docs.astral.sh](https://docs.astral.sh/ruff/formatter/?utm_source=openai))  \n- To rearrange (fix) the imports, you use `ruff check --select I --fix`, which runs linting focused on import rules and auto-fixes issues. ([pydevtools.com](https://pydevtools.com/handbook/how-to/how-to-sort-python-imports-with-ruff/?utm_source=openai))\n\n---\n\n## How to trigger import sorting in VS Code with the Ruff extension\n\nThe extension provides several ways:\n\n1. **Command palette / explicitly run a command**  \n   The command registered by the extension is called `ruff.executeOrganizeImports`, labelled \u201cFormat imports\u201d. You can open Command Palette (`Ctrl+Shift+P` / `Cmd+Shift+P`) and run \u201cFormat Imports\u201d via that. ([deepwiki.com](https://deepwiki.com/astral-sh/ruff-vscode/4.3-import-organization?utm_source=openai))\n\n2. **Using the built-in \u201cOrganize Imports\u201d action**  \n   The Ruff extension can handle the `source.organizeImports` action in VS Code. If this setting is enabled (`ruff.organizeImports` default is `true`), Ruff acts as the implementer when you trigger \"Organize Imports\" (via the command palette or context menu). ([deepwiki.com](https://deepwiki.com/astral-sh/ruff-vscode/4.3-import-organization?utm_source=openai))\n\n3. **Code Actions (lightbulb menu or auto suggestions)**  \n   Ruff provides code actions via its LSP/extension that will suggest sorting imports when violations are detected. You can accept those via the lightbulb or via VS Code\u2019s code actions. ([deepwiki.com](https://deepwiki.com/astral-sh/ruff-vscode/4.3-import-organization?utm_source=openai))\n\n4. **On\u2010save automation**  \n   You can configure VS Code to automatically organize imports when saving a Python file. That uses `editor.codeActionsOnSave`, enabling `source.organizeImports`. Ruff\u2019s extension picks up that \u201corganize imports\u201d on save action if configured. ([deepwiki.com](https://deepwiki.com/astral-sh/ruff-vscode/4.3-import-organization?utm_source=openai))\n\n---\n\n## What settings you might need\n\nTo get everything working smoothly, you may need:\n\n- In your **Ruff configuration file** (e.g. `pyproject.toml` or `ruff.toml`), include rules to select import sorting:\n\n```toml\n[tool.ruff]\nextend-select = [\"I\"]\n```\n\nThis ensures import sorting rules are active. ([deepwiki.com](https://deepwiki.com/astral-sh/ruff-vscode/4.3-import-organization?utm_source=openai))\n\n- In VS Code\u2019s settings (User or Workspace), enable Ruff to take over the organize imports action. For example:\n\n```json\n{\n  \"ruff.organizeImports\": true,\n  \"[python]\": {\n    \"editor.codeActionsOnSave\": {\n      \"source.organizeImports\": true\n    }\n  }\n}\n```\n\nThis ensures that saving a file triggers Ruff\u2019s import organizing. ([deepwiki.com](https://deepwiki.com/astral-sh/ruff-vscode/4.3-import-organization?utm_source=openai))\n\n---\n\n## Example workflow to sort imports in a file\n\n1. Make sure Ruff is installed in your project and the VS Code extension is enabled.  \n2. Ensure your Ruff config enables the `I`-rules.  \n3. Use any of these:\n   - Run **\u201cFormat imports\u201d** command via Command Palette \u2192 Ruff executes import sorting.\n   - Select \u201cOrganize Imports\u201d (default VS Code command) when in a Python file.\n   - Accept a code action suggestion for sorting imports.  \n4. Optional: save file (if `editor.codeActionsOnSave` is configured) to auto-invoke.\n\n---\n\nIf you like, I can walk you through how to set this up from scratch (installing the extension, configuring Ruff, modifying VS Code settings) with a ready\u2010to\u2010copy setup."
    }
]