You can create such a VSCode Task by yourself.
1. Install autoflake
pip install autoflake
2. Create Vscode Task
Option 1. Without activate
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "autoflake.removeUnusedImports",
"command": "${command:python.interpreterPath}",
// in Git Bash, "'${command:python.interpreterPath}'",
"args": [
"-m"
"autoflake",
"--in-place",
"--remove-all-unused-imports",
"${file}",
// in Git Bash, "'${file}'",
// to run on all files in the working directory, replace "${file}", with "--recursive", "."
],
"presentation": {
"echo": true,
"reveal": "silent",
"focus": false,
"panel": "dedicated",
"showReuseMessage": false,
"clear": false,
"close": true
},
"problemMatcher": []
},
]
}
Option 2. With activate
The above method (running autoflake as a module) works at least on Windows (works in PowerShell and Command Prompt, Git Bash), and may work on other operating systems or environments. (I don't know because I don't have one. Please edit.) Alternatively, you can use activate
.
PowerShell
"command": "${command:python.interpreterPath}\\..\\activate.ps1\r\n",
"args": [
"autoflake",
"--in-place",
"--remove-all-unused-imports",
"${file}",
],
Command Prompt
"command": "${command:python.interpreterPath}\\..\\activate &&",
"args": [
"autoflake",
"--in-place",
"--remove-all-unused-imports",
"${file}",
],
Bash
In Bash, it seems that file paths must be enclosed in quotation marks.
"command": "source",
"args": [
"\"${command:python.interpreterPath}\\..\\activate\"\r\n"
"autoflake",
"--in-place",
"--remove-all-unused-imports",
"\"${file}\"",
],
3. Add the task to keyboard shortcuts (Optional)
- Press CtrlShiftP and select
Preferences: Open Keyboard Shortcuts (JSON)
.
- Add the following settings.
[
{
"key": "Shift+Alt+P",//Set this value to any you like.
"command": "workbench.action.tasks.runTask",
"args": "autoflake.removeUnusedImports",
}
]
In this way, pressing the shortcut key will automatically delete unused imports.
Unfortunately, vscode-autoflake
did not work in to my environment for some reason.