I would:
1. create a venv, empty, without global packages.
2. pip install bloater
3. pip freeze | tee alldeps.txt
remove bloater
from alldeps.txt
4. grep who uses what using bash:
(I'd use ripgrep for that, easier recursion to understand than grep)
I stole the loop code from this answer (I don't think I've looped on file contents before).
touch imported.txt
while read package; do
printf "found:$package\n" | tee -a imported.txt
rg -t py -l $package | tee -a imported.txt
done <alldeps.txt
Note: rg -t py -l "$package.+import"
and rg -t py -l "import.+$package"
, together, might be better at only seeing imports.
Anything without actual python files in imported.txt can be put into 2nd file, todelete.txt
5 loop deletion
touch uninstall.log
while read package; do
pip uninstall $package | tee -a uninstall.log
done <todelete.txt
6 run unit tests and/or use package
One problem I see with this approach is the unclear relationship between package names and import names.
for example:
% pip list | egrep -i yaml
PyYAML 6.0
but
from yaml import dump