Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect unused imports in visual studio code for python 3?

I am subscribing to this issue about warning unused imports for python in visual studio code.

I am not particular about whether it's squiggle or gray out. But I am not certain if this feature is available based on the discussion or there's a workaround using a linter.

I am okay either way so long some detection is available.

I am using pylint as linter for now. Would be okay to use other linter so long I can detect unused imports.

And I do not want to auto remove unused imports.

This is what I see despite turning on pylint. I have purposely added an unused import. And I am not seeing any problems in this file.

enter image description here

like image 436
Kim Stacks Avatar asked Nov 18 '18 03:11

Kim Stacks


People also ask

How do I find unused imports in Python?

Importchecker is a commandline utility to find unused imports in Python modules. Its output is “grep-like” (and thus “emacs-friendly”), reporting both the module's filenames and line numbers where names are imported that are not acually used in the module. Importchecker will not modify any of the source files.

How do I find unused codes in VSCode?

To find unused members with a Code Analysis Ruleset, from the Visual Studio menu select File -> New -> File… -> General -> Code Analysis Rule Set. Uncheck all the rules.


2 Answers

Update/create VSCode user settings

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

this works for me in Python 3.6.7 / 3.6.8

like image 145
viru Avatar answered Sep 18 '22 05:09

viru


The Python extension for VS Code does not support warning about unused imports in its language server yet. But if you want Pylint to warn you, create a .pylintrc and and turn on the W0611 warning.

like image 43
Brett Cannon Avatar answered Sep 18 '22 05:09

Brett Cannon