How to Remove Unused Packages from Virtualenv

How can I remove unused packages from virtualenv?

Making virtualenvs is relatively cheap. You could just create a new virtualenv whenever you get into this situation and run your pip install again.

Not very elegant, but it gets the job done. Of course you need to be maintaining some requirements file for the pip install and it will go faster if you have some local index or cache for pip.

Virtualenv - Cleaning up unused package installations

A method I use is with my requirements.txt files. From the root of my Django project, I create a requirements/ directory with the following files in it:

requirements/
base.txt
dev.txt
prod.txt
temp.txt

base.txt contains packages to be used in all environments such as Django==1.8.6.

Then dev would include base and other packages, and might look like:

-r base.txt
coverage==4.0.2

Then temp.txt includes dev.txt and contains packages I'm not sure I'll use permanently:

-r dev.txt
temp_package==1.0
git+https://github.com/django/django.git#1014ba026e879e56e0f265a8d9f54e6f39843348

Then I can blow away the entire virtualenv and reinstall it from the appropriate requirements file like so:

pip install -r requirements/dev.txt

Or, to include the temp_package I'm testing:

pip install -r requirements/temp.txt

That's just how I do it, and it helps keep my sandbox separate from the finished product.

How can I 'clean up' a virtualenv?

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

What is the easiest way to remove all packages installed by pip?

I've found this snippet as an alternative solution. It's a more graceful removal of libraries than remaking the virtualenv:

pip freeze | xargs pip uninstall -y

In case you have packages installed via VCS, you need to exclude those lines and remove the packages manually (elevated from the comments below):

pip freeze | grep -v "^-e" | xargs pip uninstall -y


Related Topics



Leave a reply



Submit