I am working on some old python code and I was wondering if there is an easy way to find out which modules are imported but never used. I have a few hundred python scripts, each importing tens of modules (most scripts where copy-pasted by some template).
Asked
Active
Viewed 131 times
-1
-
1This is normally the job of a *linter* tool. [Flake8](https://pypi.org/project/flake8/) would certainly deserve a try. – Serge Ballesta Oct 19 '20 at 08:49
2 Answers
3
Pylint can do this. It reports "Unused import <module>" (warning named unused-import
, code W0611).
This warning is enabled by default along with many others, but if you want to check for this warning specifically, you can do:
pylint --disable=all --enable=unused-import *.py

Thomas
- 174,939
- 50
- 355
- 478