簡體   English   中英

在 python 3 的 Visual Studio 代碼中檢測未使用的導入?

[英]Detect unused imports in visual studio code for python 3?

我正在訂閱這個關於在 Visual Studio 代碼中警告未使用的 python 導入的問題。

我並不特別關注它是波浪線還是灰色。 但我不確定是否可以根據討論使用此功能,或者是否有使用 linter 的解決方法。

無論哪種方式我都很好,只要有一些檢測可用。

我現在使用 pylint 作為 linter。 只要我可以檢測到未使用的導入,就可以使用其他 linter。

而且我不想自動刪除未使用的導入。

盡管打開了pylint,這就是我所看到的。 我特意添加了一個未使用的導入。 而且我在這個文件中沒有看到任何問題。

在此處輸入圖像描述

更新/創建 VSCode 用戶設置

"python.linting.pylintEnabled": true,
"python.linting.pylintArgs": [
    "--enable=W0614"
]

這在 Python 3.6.7 / 3.6.8 中對我有用

VS Code 的 Python 擴展尚不支持有關其語言服務器中未使用導入的警告。 但是,如果您希望.pylintrc警告您,請創建一個.pylintrc並打開W0611警告

Visual Studio Code 於去年 2018 年4 月發布了一項新功能,允許用戶更改其設置以在保存時自動刪除unused imports 就我個人而言,我嘗試配置JavaScriptTypeScript並且它開箱即用,所以我假設它也適用於Python,因為您可以使用語言設置啟用/禁用,請嘗試更新您的setting.json文件以下新變化:

"editor.formatOnSave": true,
"[python]": {
    "editor.codeActionsOnSave": {
        "source.organizeImports": true
    }
},

希望這可以適用於您的 Python 項目! 祝你好運!

它只突出顯示未使用的導入,但不會自動刪除它們。

"python.linting.pylintEnabled": true,
"python.linting.pylintArgs": [
"--enable=W0614"],
"[python]": {
"editor.codeActionsOnSave": {
  "source.organizeImports": true
}},

版本:1.37.1(用戶設置)日期:2019-08-15T16:17:55.855Z Electron:4.2.7 Chrome:69.0.3497.128 Node.js:10.11.0 V8:6.9.427.31:Windows_NT.0 OS x64 10.0.16299

你可以試試這個:

  • 安裝pylance擴展
  • 用戶/工作區設置languageServer
     { "python.languageServer": "Pylance" }

我已經在 VSCode 中安裝了 autoflake 插件( https://github.com/PyCQA/autoflake ),並添加了快捷鍵CTRL + SHIFT + i 每當我按CTRL + SHIFT + i時,都會刪除未使用的導入。

安裝 autoflake 后,您可以在 VSCode 上按CTRL+SHIFT+p找到它,然后鍵入autoflake 然后,您可以設置快捷方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM