[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Best way to remove unused pip installed modules/module dependencies from a virtual env?

Looking for advice on the best way to remove unused modules from a
Python virtual environment. My setup is Python 3.6.6 running on macOS
although I believe my use case is OS independent.
Background: Long running project that, over the course of time, pip installed modules that are no longer used by the code. I'm looking for a way to identity unused modules and remove them. 
Here's my back-of-napkin strategy to do this. Wondering if there are
holes in this approach or if there's an off-the-shelf solution for
my use case?
1. pip freeze > modules.txt
2. build a list of all import statements, extract out module names
3. remove these module names from modules.txt and add to used-
   modules.txt4. modules that remain in modules.txt are either module dependencies of
   directly imported modules or no longer used5. remove my virtual env and recreate it again to start with a fresh env6. reinstall each directly imported module (from list in used-
   modules.txt); this will pull in dependencies again7. pip freeze > requirements.txt <--- this should be the exact modules
   used by our code
Thank you,