I have a Python code file with many functions in it. I'm only using a few. Is there a tool available to remove all of the unused ones for me?
Asked
Active
Viewed 2,213 times
-1
-
By running WHAT Python program? – CryptoFool Mar 21 '19 at 03:10
-
What do you mean by "remove the functions"? Do you mean actually directly edit the source files to remove them? – Pika Supports Ukraine Mar 21 '19 at 03:11
-
Maybe you're looking for something like this: https://github.com/jendrikseipp/vulture\ – CryptoFool Mar 21 '19 at 03:12
-
yes...and only in these custom python files. – Wrp Yuexia Mar 21 '19 at 03:12
-
You keep saying things like "this" and "these", but I see no code or references to code. – CryptoFool Mar 21 '19 at 03:14
-
It is just a python program with many functions, but I only need one or two function in it... – Wrp Yuexia Mar 21 '19 at 03:17
-
Steve, I think the link you post here is actually what I need...Thank you very much~ – Wrp Yuexia Mar 21 '19 at 03:19
-
1Does this answer your question? [How can you find unused functions in Python code?](https://stackoverflow.com/questions/693070/how-can-you-find-unused-functions-in-python-code) – user76284 Apr 20 '23 at 19:10
1 Answers
4
As Steve mentioned in the comments, you could use Vulture.
Here's an example from the documentation
Consider the following Python script (dead_code.py):
import os class Greeter: def greet(self): print("Hi") def hello_world(): message = "Hello, world!" greeter = Greeter() greet_func = getattr(greeter, "greet") greet_func() if __name__ == "__main__": hello_world()
Calling
vulture dead_code.py
results in the following output:
dead_code.py:1: unused import 'os' (90% confidence) dead_code.py:4: unused function 'greet' (60% confidence) dead_code.py:8: unused variable 'message' (60% confidence)

MarredCheese
- 17,541
- 8
- 92
- 91
-
After reading this post i installed vulture and the hardest thing to do was to add it to my tools list in pyscripter. Works like a charm – Peter Hedlund Aug 28 '19 at 16:01