Reputation: 701
I'm working on a fairly large Python project with a large number of modules in the main repo. The problem is that over time we have stopped using many of these modules without deleting or moving the .py file. So the result is we have far more files in the directory than we are actually using, and I would like to remove them before porting the project to a new platform to avoid porting code we don't need.
Is there a tool that lets me supply the initial module (that starts the application) and a directory, and tells me which files are imported and which aren't? I've seen many similar tools that look at imports and tell me if they are used or not, but none that check files to see if they were imported to begin with.
Upvotes: 4
Views: 614
Reputation: 101
You can retrieve loaded modules at runtime by reading sys.modules
. This is a dictionary containing module names as keys and module
objects as values. From module object you can get the path to the loaded module.
But be aware: somewhere in you project modules can be loaded in runtime. In this case you can try to find usages of __import__(...)
function and importlib.import_module(...)
.
Upvotes: 2