5

How do I get all the unused variables and functions in my Python project?

noamt
  • 7,397
  • 2
  • 37
  • 59
NGB
  • 400
  • 3
  • 16

2 Answers2

8

Use pylint!

Install it with

pip install pylint

And run it on your project files

find . -name "*.py" | xargs pylint

The above command will find all Python source files in your project and feed them using xargs to pylint. Pylint with output a report containing all lint warnings including unused variables.

noamt
  • 7,397
  • 2
  • 37
  • 59
  • Doesn't work for me. I even explicitly added a global variable which I know is not used anywhere, and `pylint` didn't mention it. – Hi-Angel Dec 04 '20 at 08:17
  • @Hi-Angel perhaps the py file wasn't found? Does it not work even with `pylint path/to/your/file.py` ? – noamt Dec 06 '20 at 09:34
  • It was found. When I pass the file alone to pylint, it only prints a `Missing module docstring (missing-module-docstring)`. As a matter of fact, if I create a file `test.py` with a single line `FOO_BAR = 'foo_bar'`, then run `pylint test.py`, I only get the warning about docstring. I thought, perhaps `pylint` generated bad ` ~/.pylintrc` file, and tried renaming it to ` ~/.pylintrcBCKP`, but it changed nothing. pylint version 2.6.0 here. – Hi-Angel Dec 06 '20 at 10:50
0

Can use vulture. These come in handy.It can't catch everything due to Python's dynamic nature, but can identify a lot without using the test suite completely.

rajkris
  • 1,775
  • 1
  • 9
  • 16