How can I use a pip requirements file to uninstall as well as install packages?

Question:

I have a pip requirements file that changes during development.

Can pip be made to uninstall packages that do not appear in the requirements file as well as installing those that do appear? Is there a standard method?

This would allow the pip requirements file to be the canonical list of packages – an ‘if and only if’ approach.

Update: I suggested it as a new feature at https://github.com/pypa/pip/issues/716

Asked By: wodow

||

Answers:

It’s not a feature of pip, no. If you really want such a thing, you could write a script to compare the output of pip freeze with your requirements.txt, but it would likely be more hassle than it’s worth.

Using virtualenv, it is easier and more reliable to just create a clean environment and (re)install from requirements.txt, like:

deactivate
rm -rf venv/
virtualenv venv/
source venv/bin/activate
pip install -r requirements.txt
Answered By: dbr

The short answer is no, you can’t do that with pip.

Answered By: Michael Mior

This should uninstall anything not in requirements.txt:

pip freeze | grep -v -f requirements.txt - | grep -v '^#' | xargs pip uninstall -y

Although this won’t work quite right with packages installed with -e, i.e. from a git repository or similar. To skip those, just filter out packages starting with the -e flag:

pip freeze | grep -v -f requirements.txt - | grep -v '^#' | grep -v '^-e ' | xargs pip uninstall -y

Then, obviously:

pip install -r requirements.txt

Update for 2016:
You probably don’t really want to actually use the above approach, though. Check out pip-tools and pip-sync which accomplish what you are probably looking to do in a much more robust way.

https://github.com/nvie/pip-tools

Update for May, 2016:

You can now also use pip uninstall -r requirements.txt, however this accomplishes basically the opposite – it uninstalls everything in requirements.txt

Update for May, 2019:

Check out pipenv or Poetry. A lot has happened in the world of package management that makes this sort of question a bit obsolete. I’m actually still quite happily using pip-tools, though.

Answered By: Stephen Fuhry

Piggybacking off @stephen-j-fuhry here is a powershell equivalent I use:

pip freeze | ? { $_ -notmatch ((gc req.txt) -join "|") }
Answered By: egbutter

Stephen’s proposal is a nice idea, but unfortunately it doesn’t work
if you include only direct requirements in your file, which sounds
cleaner to me.

All dependencies will be uninstalled,
including even distribute, breaking down pip itself.

Maintaining a clean requirements file while version tracking a virtual environment

Here is how I try to version-track my virtual environment.
I try to maintain a minimal requirements.txt, including only
the direct requirements, and not even mentioning version constraints where
I’m not sure.

But besides, I keep, and include in version tracking (say git),
the actual status of my virtualenv in a venv.pip file.

Here is a sample workflow:


setup virtualenv workspace, with version tracking:

mkdir /tmp/pip_uninstalling
cd /tmp/pip_uninstalling
virtualenv venv
. venv/bin/activate

initialize version tracking system:

git init
echo venv > .gitignore
pip freeze > venv.pip
git add .gitignore venv.pip
git commit -m "Python project with venv"

install a package with dependencies, include it in requirements file:

echo flask > requirements.txt
pip install -r requirements.txt
pip freeze > venv.pip

Now start building your app, then commit and start a new branch:

vim myapp.py
git commit -am "Simple flask application"
git checkout -b "experiments"

install an extra package:

echo flask-script >> requirements.txt
pip install -r requirements.txt
pip freeze > venv.pip

… play with it, and then come back to earlier version

vim manage.py
git commit -am "Playing with flask-script"
git checkout master

Now uninstall extraneous packages:

pip freeze | grep -v -f venv.pip | xargs pip uninstall -y

I suppose the process can be automated with git hooks, but let’s not go off topic.

Of course, it makes sense then to use some package caching system
or local repository like pip2pi

Answered By: RockyRoad

It is possible now using:

pip uninstall -r requirements.txt
Answered By: Roberto Nunes

You can now pass the -r requirements.txt argument to pip uninstall.

pip uninstall -r requirements.txt -y

At least as of pip 8.1.2, pip help uninstall shows:

...
Uninstall Options:
  -r, --requirement <file>    Uninstall all the packages listed in the given requirements file.  This option can be
                              used multiple times.
...
Answered By: Shinto Joseph

While this doesn’t directly answer the question, a better alternative to requirements.txt now is using a Pipfile. This functions similarly to a Ruby Gemfile. Currently, you need to make use of the pipenv tool but hopefully this will eventually be incorporated into pip. This provides the pipenv clean command which does what you want.

(Note that you can import an existing requirements.txt with pipenv install -r requirements.txt. After this you should have a Pipfile and the requirements.txt can be removed.)

Answered By: Michael Mior

This is an old question (but a good one), and things have changed substantially since it was asked.

There’s an offhand reference to pip-sync in another answer, but deserves its own answer, because it solves precisely the OP’s problem.

pip-sync takes a requirements.txt file as input, and “trues up” your current Python environment so that it matches exactly what’s in that requirements.txt. This includes removing any packages that are present in your env but absent from requirements.txt.

Example: Suppose we want our env to contain (only) 3 libraries: libA, libB, and libC, like so:

> cat requirements.txt
libA==1.0
libB==1.1
libC==1.2

But our env currently contains libC and libD:

> pip freeze
libC==1.2
libD==1.3

Running pip-sync will result in this, which was our desired final state:

> pip-sync requirements.txt
> pip freeze
libA==1.0
libB==1.1
libC==1.2
Answered By: ron rothman

Here’s a simple solution that works:

pip uninstall $(pip freeze) -y

Answered By: danmurphy1217
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.