This is available now with a new release of the pylance extension (which I assume most people using python in VS Code will have).

It should be noted that the optimizeImports with the ctrl + alt/option + o keybinding only sorts and does not remove unused imports (see github issue).

I have autosave in place, so I prefer a keyboard shortcut. If you have the pylance extension, you can add the following to your keybindings.json

    {
        "key": "shift+alt+r",
        "command": "editor.action.codeAction",
        "args": {
            "kind": "source.unusedImports",
        }
    }

You can change the key binding to be whatever you want, but basically when you press the keybinding (i.e shift + option/alt + r) it should remove all your unused imports.

I believe if you wanted this automatically on save as above you could add the following into your settings.json:

    "[python]": {
        "editor.codeActionsOnSave": {
          "source.organizeImports": "explicit",
          "source.unusedImports": "explicit",
        }
    }
Answer from emv on Stack Overflow
🌐
Visual Studio Code
code.visualstudio.com › docs › python › editing
Editing Python in Visual Studio Code
November 3, 2021 - Enabling the full set of IntelliSense features by default could end up making your development experience feel slower, so the Python extension enables a minimum set of features that allow you to be productive while still having a performant experience. However, you can customize the behavior of the analysis engine to your liking through multiple settings. Pylance offers auto import suggestions for modules in your workspace and for packages you installed in your environment.
🌐
Donjayamanne
donjayamanne.github.io › pythonVSCodeDocs › docs › refactoring_sort-imports
Sort Imports | Python in Visual Studio Code
There are a three options to invoke the sort imports command, they are as follows: · Option 1 (Command Palette)
Discussions

python - VSCode: Sort imports vs. Organize Imports - Stack Overflow
When I enter option + shift + o on my Mac in a Python file in VSCode, I am given two options - "Sort imports" and "Organize Imports". They both organize the inputs nicely but in a different way, so... More on stackoverflow.com
🌐 stackoverflow.com
Python & VS Code: make Black and organize imports work together on save
I also suggest to configure pre-commit then, even if you use another editor, the proper formating will be forced on commit. More on reddit.com
🌐 r/Python
19
200
March 2, 2022
Python organize imports completely missing?
Hi Cursor team, I am using both vscode and cursor and find that in cursor the organize import feature is somehow not even present. CMD + SHIFT + P “organize”, “imports” does not show up at all and I am unable to set up a working shortcut that would actually organize imports. More on forum.cursor.com
🌐 forum.cursor.com
3
2
April 7, 2024
Remove unused imports on Organize Imports
When running Organize Imports, I would expect that the imports will be not only sorted but also all unused imports will be removed. Organize Imports for JavaScript in VS Code does the same. Optimiz... More on github.com
🌐 github.com
5
August 21, 2020
Top answer
1 of 11
43

This is available now with a new release of the pylance extension (which I assume most people using python in VS Code will have).

It should be noted that the optimizeImports with the ctrl + alt/option + o keybinding only sorts and does not remove unused imports (see github issue).

I have autosave in place, so I prefer a keyboard shortcut. If you have the pylance extension, you can add the following to your keybindings.json

    {
        "key": "shift+alt+r",
        "command": "editor.action.codeAction",
        "args": {
            "kind": "source.unusedImports",
        }
    }

You can change the key binding to be whatever you want, but basically when you press the keybinding (i.e shift + option/alt + r) it should remove all your unused imports.

I believe if you wanted this automatically on save as above you could add the following into your settings.json:

    "[python]": {
        "editor.codeActionsOnSave": {
          "source.organizeImports": "explicit",
          "source.unusedImports": "explicit",
        }
    }
2 of 11
31
  • Confirm you have Pylance enabled in extensions. It comes with the vscode Python extension by Microsoft.

  • In your .vscode/settings.json, add this

    "python.analysis.fixAll" : ["source.unusedImports"]

Now when you do Ctrl+Shift+P (Comand Palette) -> "Fix All" this will clean up the unused imports.

  • To auto cleanup on save, add this:
    "editor.codeActionsOnSave": {
        "source.unusedImports": true,
    }

you can also add "source.organizeImports": true in there to sort imports on save.

  • If this doesnt work try overriding any other installed language servers with:
    "python.languageServer": "Pylance",

Docs here

🌐
GitHub
gist.github.com › jvacek › efe6b6fec1bbeb25718bc89b9c1b5acc
Remove unused python imports on save in VSCode · GitHub
There are two ways to achieve this with some plugins; one via hitting the shell and doing an in-place replace, the other is by doing it with extensions configurable in vscode. Both should give you the same results.
🌐
YouTube
youtube.com › shorts › 4ao700yIzj0
Optimize imports in VSCode. - YouTube
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
Published   August 1, 2023
Find elsewhere
🌐
YouTube
youtube.com › watch
VS Code: Organize Imports - YouTube
🦸 Become A VS Code SuperHero Today: https://vsCodeHero.com🧑🏾‍🤝‍🧑🏽 Join my Discord developer community: https://discord.gg/A9CnsVzzkZI hate it when my J...
Published   December 14, 2022
🌐
Wadaef EN
en.wadaef.net › vscode-python-optimize-imports
VSCODE PYTHON OPTIMIZE IMPORTS - Wadaef
June 24, 2024 - Now that auto import optimization is enabled, you can trigger it by saving your Python file (Ctrl+S) or by using the “Optimize Imports” command from the Command Palette (Ctrl+Shift+P). VSCode will analyze your code and remove any unused ...
🌐
Reddit
reddit.com › r/python › python & vs code: make black and organize imports work together on save
r/Python on Reddit: Python & VS Code: make Black and organize imports work together on save
March 2, 2022 - What did you mean by sync? Setting minimum python version in pre-commit configuration matching to your environment? ... Hi everyone! Here’s a short blog post on getting Black code formatting and VS Code’s "organize imports on save" to play nicely together.
🌐
10xdev
10xdev.blog › vscode-automatically-organize-python-imports
VS Code: Automatically Organize Python Imports |
September 6, 2020 - "[python]": { "editor.codeActionsOnSave": { "source.organizeImports": true } } Now upon saving files with the .py extension, VS Code will automatically sort and organize the imports.
🌐
DEV Community
dev.to › shanesc › how-to-sort-and-cleanup-imports-on-save-in-vs-code-52p1
How to: Enable automatic import sorting in VS Code - DEV Community
November 22, 2024 - Fortunately, "source.sortImports" works just like "source.organizeImports" without removing the imports you haven't used yet. ... "source.organizeImports": true was giving a type error in my editor. It had to be a string rather than boolean. I put the string value "always" instead boolean value "true" and it fixed the problem. ... JS, TS, node, web development, mobile app development, mobile game development, social network game development ... Dr. at Szpak.Labs ... @nikkizol not sure about how vscode does it, but if you let eslint do the job (via eslint-plugin-imports) instead of vscode, then you can achieve this with the setting option "newlines-between": "never",.
🌐
GitHub
gist.github.com › cb109 › 47496649640c9c5f4287b06db7a7c197
VSCode: Python Sort Imports on File Save (isort) · GitHub
I only had to do 2 things. Use the vscode settings here https://eshlox.net/2019/12/02/vscode-sort-python-imports-automatically
🌐
Visual Studio Marketplace
marketplace.visualstudio.com › items
Autoflake - Remove unused Python imports - Visual Studio Marketplace
January 25, 2021 - Extension for Visual Studio Code - Remove unused and re-organize imports in Python
🌐
GitHub
github.com › microsoft › pylance-release › issues › 268
Remove unused imports on Organize Imports · Issue #268 · microsoft/pylance-release
August 21, 2020 - When running Organize Imports, I would expect that the imports will be not only sorted but also all unused imports will be removed. Organize Imports for JavaScript in VS Code does the same. Optimiz...
Author   hmoravec
🌐
iO tech_hub
techhub.iodigital.com › articles › 6-vscode-tips
6 Quick Tips to Boost Your Productivity in VSCode
June 21, 2023 - VSCode provides a handy shortcut to remove unused imports. Simply press cmd + option + o . Or Shift + Alt + o on Windows. This will remove any unnecessary imports, decluttering your codebase.
🌐
Visual Studio Marketplace
marketplace.visualstudio.com › items
reorder-python-imports - Visual Studio Marketplace
Extension for Visual Studio Code - Provides support for the reorder-python-imports tool for python
🌐
Medium
medium.com › @sebastiantolomeo › how-to-automatically-organize-imports-in-visual-studio-code-e80d87b4f263
Automatically Organize Imports in Visual Studio Code | by Sebastian Tolomeo | Medium
June 5, 2022 - I was curious as to why VS Code sorted my import statements in a specific way, so after some searching, it turns out that Organize Imports is used to remove any unused imports, sort existing imports by file paths, and sort named imports.
🌐
eshlox
eshlox.net › home › blog › it › vs code - sort python imports automatically
VS Code - sort Python imports automatically | eshlox
December 2, 2019 - How to configure VS Code to run isort automatically on every Python file save, keeping imports sorted without any manual effort.