![](/img/trans.png)
[英]How to stop VS Code from removing Python unused imports on save
[英]Is there a way to remove unused imports for Python in VS Code?
我真的很想知道 Visual Studio Code 中是否有一些擴展或其他方法可以幫助識別和刪除任何未使用的導入。
我有相當多的這樣的導入,它已經接近 40 行了。 我知道其中一些沒有被使用,問題是安全地移除它們。
from django.core.mail import EmailMultiAlternatives, send_mail
from django.template.loader import render_to_string
from django.utils.html import strip_tags
from rest_framework import routers, serializers, viewsets, status
from rest_framework.views import APIView
from rest_framework.response import Response
from django.contrib.auth.models import User
轉到用戶設置json 文件並添加以下內容:
"python.linting.pylintEnabled": true,
"python.linting.pylintArgs": [
"--enable=W0614"
]
這應該會自動刪除未使用的 python 導入。
這里有更多建議: 如何檢查許多 Python 文件中未使用的導入?
您可以自己創建這樣的VSCode 任務。
pip install autoflake
創建“.vscode/tasks.json”。
添加以下設置。
activate
(Windows) {
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "autoflake.removeUnusedImports",
"command": "${command:python.interpreterPath}",
"args": [
"-m"
"autoflake",
"-i",
"--remove-all-unused-imports",
"${file}", // to run on all files in the working directory, "--recursive", "."
],
"presentation": {
"echo": true,
"reveal": "silent",
"focus": false,
"panel": "dedicated",
"showReuseMessage": false,
"clear": false,
"close": true
},
"problemMatcher": []
},
]
}
activate
上述方法至少適用於 Windows(適用於 PowerShell 和命令提示符,但不適用於 Git Bash),但可能不適用於其他操作系統或環境。 (我不知道,因為我沒有。請編輯。)在這種情況下,使用激活可能會起作用。
"command": "${command:python.interpreterPath}\\..\\activate.ps1\r\n",
"args": [
"autoflake",
"-i",
"--remove-all-unused-imports",
"${file}",
],
"command": "${command:python.interpreterPath}\\..\\activate &&",
"args": [
"autoflake",
"-i",
"--remove-all-unused-imports",
"${file}",
],
在 Bash 中,似乎文件路徑必須用引號引起來。
"command": "source",
"args": [
"\"${command:python.interpreterPath}\\..\\activate\"\r\n"
"autoflake",
"-i",
"--remove-all-unused-imports",
"\"${file}\"",
],
按Ctrl+Shift+P
和 select Preferences: Open Keyboard Shortcuts (JSON)
。
添加以下設置。
[
{
"key": "Shift+Alt+P",//Set this value to any you like.
"command": "workbench.action.tasks.runTask",
"args": "autoflake.removeUnusedImports",
}
]
這樣,按下快捷鍵會自動刪除未使用的導入。
不幸的是,由於某種原因, vscode-autoflake
無法在我的環境中運行。
我建議將pycln
添加為預提交鈎子,它專為此任務而設計!
(它僅適用於 Python 3.6+)。
文檔: https : //hadialqattan.github.io/pycln
回購: https : //github.com/hadialqattan/pycln
PyPI: https ://pypi.org/project/pycln/
有趣的是,接受的答案並沒有解決這個問題——如何刪除未使用的導入。
Pylint 不會修改代碼,它會進行 linting。
不可否認,我仍然沒有找到一個很好的 python 解決方案,但這是我所看到的:
正如這個答案中所指出的,VSCode 有一個基本的內置選項來自動組織導入,對我來說效果不佳 - 您的里程可能會有所不同:
選項+ Shift + O適用於 Mac
Alt + Shift + O
如果這對您有用,您還可以使用以下方法保存在 VSCodes 設置中:
"editor.codeActionsOnSave": {
"source.organizeImports": true
}
一個名為autoflake的模塊可以做到這一點,例如:
autoflake --in-place --remove-unused-variables example.py
但同樣,里程可能會有所不同..
我在 vscode github 中看到一個問題,指出“快速修復”功能已損壞,vscode 團隊表示這是 vscode python 插件的問題.. 可能很快就會修復..?
目前在 VSCode 上沒有明確的方法可以做到這一點,但是您可以輕松地使用pycln來做到這一點,只需執行以下操作:
pip3 install pycln
pycln path_of_your_file.py -a
然后所有未使用的導入將被刪除!
現在可以使用新版本的 pylance 擴展(我假設大多數在 VS Code 中使用 python 的人都會有)。
應該注意的是,帶有ctrl + alt/option + o
鍵綁定的optimizeImports
只會排序,不會刪除未使用的導入(請參閱github 問題)。
我有自動保存功能,所以我更喜歡鍵盤快捷鍵。 如果你有 pylance 擴展,你可以將以下內容添加到你的鍵綁定中。json
{
"key": "shift+alt+r",
"command": "editor.action.codeAction",
"args": {
"kind": "source.unusedImports",
}
}
您可以將鍵綁定更改為任何您想要的,但基本上當您按下鍵綁定(即shift + option/alt + r
)時,它應該刪除所有未使用的導入。
我相信如果你想要像上面那樣自動保存,你可以將以下內容添加到你的設置中。json:
"[python]": {
"editor.codeActionsOnSave": {
"source.organizeImports",
"source.unusedImports"
}
}
通過這個最近創建的 VSCode 擴展,autoflake 通過單擊資源管理器(在 VSCode 中)選項卡的上下文菜單或從命令面板調用命令以刪除未使用的導入來自動運行。
https://marketplace.visualstudio.com/items?itemName=mikoz.autoflake-extension
(由於我之前的回答不知何故被版主刪除了,所以我發布了一個新的哈哈。)
我承認這充其量只是一種解決方法,但是如果您想要此功能,Pycharm 和 IntelliJ 會使用優化導入熱鍵(MacOS 上的ctrl + opt + o )自動執行此操作。
autoflake vscode 擴展刪除了未使用的導入(而不僅僅是突出顯示它們或對它們進行排序)。
pip install autoflake
(這將被擴展使用)。settings.json
:{
"saveAndRunExt": {
"commands": [
{
"match": ".*\\.py",
"isShellCommand": false,
"cmd": "autoflake.removeUnused"
},
]
},
}
我對解決這個問題非常感興趣,最終創建了一個 VSCode 擴展。 您現在可以使用它了。
https://marketplace.visualstudio.com/items?itemName=mikoz.autoflake-extension
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.