Search code examples
pythonlegacy

reduce namespace pollution in python scripts


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).


Solution

  • 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