44

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?

1
  • 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, 2016 at 4:26

10 Answers 10

51

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
2
  • 1
    That is the best thing I have seen all week. Thanks!!!
    – Jim Bray
    May 10, 2021 at 1:49
  • 26
    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 Dec 22, 2021 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
4
  • 2
    "Delete the contents of the environment directory if it already exists, before environment creation." Mar 5, 2022 at 8:40
  • that didn't work for me...
    – Liker
    May 3 at 18:51
  • Error: [Errno 40] Too many levels of symbolic links: [path to the venv]
    – Liker
    May 3 at 18:55
  • @Liker777: I just checked. It still works fine on Python 3.10. What did you do? System, full command? May 3 at 19:49
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
4
  • 7
    Don't forget to remove pip-autoremove when done! :->
    – aghast
    Jan 29, 2016 at 3:48
  • Why would I remove pip autoremove when I am done instead of just leaving it there for future use?
    – Joff
    Jan 29, 2016 at 4: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, 2017 at 13:56
  • 1
    @fanny pip uninstall doesn't remove any dependencies at all. Jul 3, 2017 at 4: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.)

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

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
3
pip freeze | xargs pip uninstall -y

(Inspired by Tripp Kinetics' answer)

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.

0

You can use this command to uninstall all packages easily

pip uninstall -y $(pip freeze)
-1

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

Python Documentation

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.