42

If I want to keep my venv as clean as possible how can I clean up the stuff I don't need? Let me lay out an example...

Say I try a bunch of new modules...

pip install foo
pip install bar
pip install foobar
pip install foobarfoo

and these modules have some requirements of their own, etc. later I decide on which one I want to use, but then I have a huge list of stuff in my requirement.txt and I can't remember what I need and what I don't, what depends on what, etc.

How can I keep it clean and lean?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Joff
  • 11,247
  • 16
  • 60
  • 103
  • the whole point is I am not sure of what I need based on dependencies. Of course I could delete the whole thing, let my processes fail and install as I go, but I am looking for an easier way – Joff Jan 29 '16 at 04:26

10 Answers10

46

To uninstall every package (including dependencies) you can freeze the requirements and then pass them to pip uninstall:

pip freeze > to-uninstall.txt
pip uninstall -r to-uninstall.txt
Steve Rossiter
  • 2,624
  • 21
  • 29
  • 1
    That is the best thing I have seen all week. Thanks!!! – Jim Bray May 10 '21 at 01:49
  • 21
    This is a great solution. However, for larger virtual environments it can be annoying to have to confirm the removal of each package. In that event, I'd recommend using: `pip uninstall -y -r to-uninstall.txt` which bypasses the prompt – James Rocker Dec 22 '21 at 10:56
18

The following works for me (can be executed from any Python 3.6 virtualenv):

virtualenv --clear your-env-name

Where your-env-name could be:

  • Path to the virtual environment (relative from current directory or absolute)
  • Or if you use virtualenv-wrapper, just the name of the environment

If that doesn't work for you somehow, there's is this less convenient one-liner:

pip freeze | while read p; do pip uninstall -y "$p"; done
Artur Barseghyan
  • 12,746
  • 4
  • 52
  • 44
9

This answer may be just what you need.

You can install and use the pip-autoremove utility to remove a package plus unused dependencies.

# install pip-autoremove 
pip install pip-autoremove
# remove "somepackage" plus its dependencies: 
pip-autoremove somepackage -y
Community
  • 1
  • 1
Brendan Abel
  • 35,343
  • 14
  • 88
  • 118
  • 7
    Don't forget to remove pip-autoremove when done! :-> – aghast Jan 29 '16 at 03:48
  • Why would I remove pip autoremove when I am done instead of just leaving it there for future use? – Joff Jan 29 '16 at 04:26
  • I wonder if pip uninstall wouldn't do... When I'm removing with pip-autoremove, there are some dependencies which pip-autoremove cannot deal with (it seems that the order of deleting is important) while pip uninstall works fine. – fanny Jun 28 '17 at 13:56
  • 1
    @fanny pip uninstall doesn't remove any dependencies at all. – Brendan Abel Jul 03 '17 at 04:58
8

Following the answers from @Steve Rossiter and @James Rocker one could also do a trivial modification and avoid generating a temp file in a one-liner like:

pip uninstall -y -r <(pip freeze)

(I wanted to post this as a comment on the answer, but I don't have enough reputation for that.)

KevinYanesG
  • 171
  • 1
  • 6
7

You can use pip-tools's pip-sync functionality to keep your environment clean.

From pip-tools' documentation:

Now that you have a requirements.txt, you can use pip-sync to update your virtual environment to reflect exactly what's in there. This will install/upgrade/uninstall everything necessary to match the requirements.txt contents.

Just install pip-tools and call the pip-sync command:

pip install pip-tools
pip-sync requirements.txt

(side note: pip-tools is also great to manage you dependency versions to make your builds predictable and deterministic; see pip-tools' documentation for more information)

Fable
  • 371
  • 1
  • 4
  • 11
7

To slightly improve on another answer

Use pip but add -y to avoid prompts for every library.

Don't forget to remove the file "to_uninstall.txt" when done!

pip freeze > to_uninstall.txt
pip uninstall -y -r to_uninstall.txt
RLNYC
  • 89
  • 1
  • 4
2
pip freeze | xargs pip uninstall -y

(Inspired by Tripp Kinetics' answer)

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Paul
  • 3,920
  • 31
  • 29
1

I just wrote a one-liner for this:

pip list | tail +3 | cut -f1 -d\  | xargs pip uninstall -y

Afterwards, you probably would want to:

python -m ensurepip --upgrade

To get pip back.

Tripp Kinetics
  • 5,178
  • 2
  • 23
  • 37
0

You can use this command to uninstall all packages easily

pip uninstall -y $(pip freeze)
Msvstl
  • 1,116
  • 5
  • 21
-1

pip uninstall followed by one or more package names will remove the packages from the virtual environment.

Python Documentation

sujit
  • 63
  • 6