I'd like to clean up the code in a python package which has some unused class/function definitions. Is there a simple way to list all the functions of a package that are not called within any of the modules of that package? Assume that the 'private' classes/functions (that I want to clean up) are prefixed with an underscore so they are not confused with 'public' API.
Asked
Active
Viewed 132 times
2 Answers
2
Several suggestions:
- If you are using an IDE like PyCharm, it will gray out the unused imports.
- If you care to install a lint tool like pylint, it will warn you of your unused imports.
- A poor man's way would be to comment out all your import statements and see what module invocations complain. You could add them back in, one at a time.

rajah9
- 11,645
- 5
- 44
- 57
-
FWIW: I also recommend **pylint**. It's built into Spyder and very easy to use. The outputs can be parsed easily with GNU tools, for a bit of customised code analysis. – S3DEV Sep 26 '19 at 12:46
-
If you are using PyCharm, Code -> Organize Inputs will both remove unused imports and arrange them. – rajah9 Oct 30 '19 at 12:23
1
You can find class/functions/vars usages using Pycharm IDE by pressing Ctrl+LeftMouseButton
on class/functions/vars names to search for usages. If no usages where found in your project, simply delete the dead code manually.

arturkuchynski
- 790
- 1
- 9
- 27