I’ve recently been trying to use best practices when installing packages:
- Only installing global packages when it makes sense.
- Using virtual environments 99% of the time.
- Uninstalling packages whenever they are no longer needed.
I find this to work well for me. However, #3 is a pain when uninstalling packages that have a ton of dependencies, like tensorflow
. In comes, pip-autoremove
to the rescue.
The issue with pip-autoremove
is that it removes everything. Here’s a scenario:
- I
pip install matplotlib
because I need to show some plots. - I then realize that maybe
seaborn
could fit my needs a little better, so Ipip install seaborn
. - I realize that I was wrong and
matplotlib
will actually fit my needs just fine so Ipip-autoremove seaborn
. - Uh, oh! That removed
seaborn
ANDmatplotlib
! - Now, I have to
pip install matplotlib
again before I can continue development.
Is there a better solution out there to this specific problem? Or, is this more of me needing to change my philosophy on package management?
>Solution :
I usually recommend using Poetry to handle your virtual environments. Poetry creates a single .venv per project and tracks all your dependencies with two files that can be tracked by git, pyproject.toml
and poetry.lock
.
When you remove with poetry a dependency (seaborn) that also depends on another installed dependency (matplotlib), poetry will remove all the extra dependencies that seaborn has installed and rollback the matplotlib dependencies to their previously installed version.