How do I get all the unused variables and functions in my Python project?
Asked
Active
Viewed 5,802 times
5
-
Look into [Pylint](https://www.pylint.org/). – Paco H. Oct 03 '17 at 07:21
-
What IDE are you using for programming? – Matthias Burger Oct 03 '17 at 07:23
-
And/or a good IDE like PyCharm which will highlight unused variables while you code. – Guillaume Oct 03 '17 at 07:23
-
I am using wing IDE – NGB Oct 03 '17 at 07:24
-
@MatthiasBurger actually my organization has restricted the use of any other IDE, thats why – NGB Oct 03 '17 at 07:28
2 Answers
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