I looking for a way to audit and minimize requirements.txt. I've taken over a project that has grown bloated over several iterations, and I'm trying to make it more maintainable. The current virtual environment I'm in was created from the previous requirements.txt; that file has packages that are no longer imported in any script.
In the past, I've done this manual process:
- Search through project directory to find all python files in all subfolders
- Search through each python file found to find all
import x
andfrom x import y
statements, add those packages to a list pip show
each of packages on the list, adding any dependencies to the end of the list- Once list is exhausted, sort and compare to requirements.txt
- Remove requirements that aren't on the list.
Assuming that my code performs no relative imports, is there a way to automate this process? I can't imagine I'd be the first person looking for such a tool (or gist, or script). I couldn't find any. I use windows, but I'm happy to linux commands on windows subsystem for linux.