本文翻译自:What is the easiest way to remove all packages installed by pip?
I'm trying to fix up one of my virtualenvs - I'd like to reset all of the installed libraries back to the ones that match production. 我正在尝试修复我的一个virtualenvs-我想将所有已安装的库重置为与生产版本匹配的库。
Is there a quick and easy way to do this with pip? 有没有一种快速简便的方法来使用pip?
#1楼
参考:https://stackoom.com/question/lC8X/删除pip安装的所有软件包的最简单方法是什么
#2楼
The quickest way is to remake the virtualenv completely. 最快的方法是完全重新制作virtualenv。 I'm assuming you have a requirements.txt file that matches production, if not: 我假设您有一个匹配生产的requirements.txt文件,如果不匹配:
# On production:
pip freeze > reqs.txt
# On your machine:
rm $VIRTUALENV_DIRECTORY
mkdir $VIRTUALENV_DIRECTORY
pip install -r reqs.txt
#3楼
I've found this snippet as an alternative solution. 我已找到此代码段作为替代解决方案。 It's a more graceful removal of libraries than remaking the virtualenv: 与重建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): 如果您通过VCS安装了软件包,则需要排除这些行并手动删除软件包(从下面的注释中升高):
pip freeze | grep -v "^-e" | xargs pip uninstall -y
#4楼
This works with the latest 这适用于最新
virtualenv --clear MYENV
But usually I just delete and recreate the virtualenv since immutability rules! 但是通常由于不变性规则,我只是删除并重新创建virtualenv!
#5楼
Cross-platform support by using only pip : 仅使用pip跨平台支持:
#!/usr/bin/env python
from sys import stderr
from pip.commands.uninstall import UninstallCommand
from pip import get_installed_distributions
pip_uninstall = UninstallCommand()
options, args = pip_uninstall.parse_args([
package.project_name
for package in
get_installed_distributions()
if not package.location.endswith('dist-packages')
])
options.yes = True # Don't confirm before uninstall
# set `options.require_venv` to True for virtualenv restriction
try:
print pip_uninstall.run(options, args)
except OSError as e:
if e.errno != 13:
raise e
print >> stderr, "You lack permissions to uninstall this package.
Perhaps run with sudo? Exiting."
exit(13)
# Plenty of other exceptions can be thrown, e.g.: `InstallationError`
# handle them if you want to.
#6楼
On Windows if your path is configured correctly, you can use: 在Windows上,如果您的path配置正确,则可以使用:
pip freeze > unins && pip uninstall -y -r unins && del unins
It should be a similar case for Unix-like systems: 对于类似Unix的系统,情况应该类似:
pip freeze > unins && pip uninstall -y -r unins && rm unins
Just a warning that this isn't completely solid as you may run into issues such as 'File not found' but it may work in some cases nonetheless 只是警告说这并不完全可靠,因为您可能会遇到诸如“找不到文件”之类的问题,但在某些情况下仍然可以使用
EDIT: For clarity: unins is an arbitrary file which has data written out to it when this command executes: pip freeze > unins 编辑:为清楚起见: unins是一个任意文件,当执行此命令时,数据已写入其中: pip freeze > unins
That file that it written in turn is then used to uninstall the aforementioned packages with implied consent/prior approval via pip uninstall -y -r unins 然后将其依次写入的文件用于通过pip uninstall -y -r unins隐含同意/事先批准的情况下卸载上述软件包。
The file is finally deleted upon completion. 该文件最终在完成后被删除。
本文讨论了如何有效地清理通过pip安装的软件包。建议的方法包括完全重建virtualenv,或者使用特定的命令行操作逐一卸载。对于包含VCS安装的软件包,可能需要手动删除。此外,提供了适用于不同操作系统的卸载方案。
7780

被折叠的 条评论
为什么被折叠?



