25

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
  • 151,199
  • 17
  • 111
  • 216
Joff
  • 9,649
  • 12
  • 50
  • 96
  • 3
    "later I decide on which one I want to use" - then create a new virtualenv and install just what you want – OneCricketeer Jan 29 '16 at 02:36
  • 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

9 Answers9

28

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,348
  • 19
  • 28
  • 1
    That is the best thing I have seen all week. Thanks!!! – Jim Bray May 10 '21 at 01:49
  • 8
    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
14

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
Artur Barseghyan
  • 10,830
  • 4
  • 48
  • 39
10

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
  • 32,182
  • 13
  • 82
  • 111
  • 6
    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
4

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
  • 51
  • 4
3

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
  • 168
  • 2
  • 9
3

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.)

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,039
  • 2
  • 22
  • 36
1
pip freeze | xargs pip uninstall -y

(Inspired by Tripp Kinetics' answer)

Gino Mempin
  • 19,150
  • 23
  • 79
  • 104
Paul
  • 3,313
  • 26
  • 28
-1

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

Python Documentation

sujit
  • 63
  • 6