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
» pip install autoflake
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",
}
}
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
Videos
I am working on a codebase that is written by multiple people in our organisation. I can see some files where some functions are imported but were never used. Now, since I am not much experienced with python, I just need some guidance regarding whether it is perfectly fine to remove them or is there a possibility that it can be a hack for some use cases ? If later is possible, please mention those cases.
PyFlakes (similar to Lint) will give you this information.
pyflakes python_archive.py
Example output:
python_archive.py:1: 'python_archive2.SomeClass' imported but unused
Now I use fast-dev-cli(base on ruff/mypy)
pip install fast-dev-cli
fast check /path/to/file-or-directory # check only
fast lint /path/to/file-or-directory # Auto fix issues and reformat
Before
I use flake8 to check the style, and then isort+autoflake to auto remove the unused imports.
Check: See more at flake8 vs pyflake
pip install flake8 --user
flake8 .
Reformat: see more at why isort+autoflake
pip install isort autoflake --user
isort -sl .
autoflake --remove-all-unused-imports -i -r .
isort -m 3 .
Recently(since 2023), I use ruff to replace autoflake/flake8
pip install --upgrade --user ruff
ruff check /path/to/file_or_folder # check only
ruff check --fix /path/to/file_or_folder # check and reformat
For newer version ruff(>=0.4) command is
ruff check --extend-select=I /path/to/file_or_folder # check only
ruff check --extend-select=I --fix /path/to/file_or_folder # check and reformat
- BTY: you can use
ruff format /path/to/file_or_folderto reformat it/them after imports cleared.
Hello everyone, I am honored to introduce a very useful tool to you
Unimport a linter & formatter for finding/removing unused import statements. It includes a CLI interface, a static analysis layer (using Python's AST module), and a fully-fledged refactoring engine (based on LibCST). Downloaded over 100k+ and battle-tested by hundreds of projects.
You can customize Unimport before using it, if you want, you can use it with pre-commit or in the docker environment. It supports Python 3.6, 3.7, 3.8, .3.9, and 3.10 versions.
Here is the documentation -> https://unimport.hakancelik.dev/
Here is the Github address -> https://github.com/hakancelikdev/unimport
I am forever open to your suggestions and any kind of contribution.