Currently a requirements file is used to specify what packages should be installed (and how, in some cases).
I believe it would be useful to have an additional option to uninstall all packages that do not appear in a supplied requirements file.
This would be useful for tightly controlling installing to a virtualenv or indeed any situation where you want to be absolutely confident that you have only the packages you desire.
Discussion on Stack Overflow: http://stackoverflow.com/questions/13176968/how-can-i-use-a-pip-requirements-file-to-uninstall-as-well-as-install-packages/
This should accomplish the functionality you're looking for in most cases (note that this doesn't seem to work with -e stuff like git/svn repositories and the like):
pip freeze | grep -v -f requirements.txt - | xargs pip uninstall -y
I would love to see such option implemented. I even think that the main use-case of requirements file is to ensure that only the listed packages are installed, not more, not less.
pip sync, perhaps?
+1 for pip sync
This would be useful in continuous integration, currently many setups start with a totally clean environment, which works, but requires re-installing everything which takes time, this would make it easy to re-use the same virtual environment, only accounting for changes in stated requirements.
Also: @fuhrysteve your provided option pip freeze | grep -v -f requirements.txt - | xargs pip uninstall -y works, sorta. It doesn't account for dependencies of dependencies (ie: mongoengine depends on pymongo, but pymongo may not be in requirements.txt, thus pymongo and mongoengine will be uninstalled.)
Thus I think something implemented in pip itself would be more prudent. As an aside, I with Github had some sort of 'vote for issues' feature.
I thought pip freeze frozed all of the packages? Including dependencies of dependencies?
Exactly. Doesn't that mean the scenario that @conslo described will never happen unless you manually edit your requirements.txt file?
@shuhaowu pip freeze lists all packages installed yes, including dependencies of dependencies. But dependencies of dependencies are NOT required to be stated in a requirements.txt file.
If I have mongengine stated as a requirement in my requirements.txt file, and I install with pip install -r requirements.txt, pymongo will also be installed (because mongoengine depends on it.) If I run @Lucretiel 's command pip freeze | grep -v -f requirements.txt - | xargs pip uninstall -y the pip freeze will list pymongoas well as mongoengine, but pymongo isn't in the requirements.txt file I'm filtering with, so it'll be passed to xargs pip uninstall -y, thus pymongo will be uninstalled, also causing mongoengine to be uninstalled.
pip install -r requirements.txt
xargs pip uninstall -y
I'm not sure what you mean by never happen unless you manually edit your requirements.txt file?, in what project are you coding that you don't control your own requirements?
never happen unless you manually edit your requirements.txt file?
@Lucretiel I'm not sure I understand. In that case wouldn't we want pip sync to remove them? If the job of pip sync is to remove packages not stated in requirements, then... packages not in requirements should be removed o.0
my use case for this would be something like when you swap out a package for a fork of that package (e.g: PIL/pillow), when updating servers with pip install -r reqs.txt successively we can come across issues where both packages remain installed (and cause conflicts). Something like pip sync (or just an option/switch on pip install) makes perfect sense to uninstall the old (and whatever dependencies) and install the new. (someone please correct me if there's a way to do this as-is)
@Lucretiel @conslo I think the issue can be overcome by splitting of dev/prod requirements, pip sync (or whatever solution) would be a good step towards encouraging this.
There's pip-sync doing this, currently under development in the https://github.com/nvie/pip-tools/ project. See http://nvie.com/posts/better-package-management/
I think another good thing would be possibility to specify in requirements that specific package should be uninstalled. I came across situation, where having django-storages==1.1.8 in requirements I decided to upgrade to django-storages-redux==1.2.2. Simple editing of requirements file is not enough, because when installing requirements the second time, both packages have the same destination directory "storages", and pip freeze reports them both instaled.
Cc @nvie who is working on pip-sync, part of pip-tools, as @piotr-dobrogost pointed out.