我试图解决我的virtualenvs之一 - 我想重置所有已安装的库回,以配合生产的人。
是否有一个快速简便的方法与PIP做到这一点?
我试图解决我的virtualenvs之一 - 我想重置所有已安装的库回,以配合生产的人。
是否有一个快速简便的方法与PIP做到这一点?
我发现这个片段作为一种替代解决方案。 这是一个更优美的去除库比重制的virtualenv的:
pip freeze | xargs pip uninstall -y
如果您有通过VCS安装的软件包,你需要排除那些线条和手动删除(从下面的评论高架)的包:
pip freeze | grep -v "^-e" | xargs pip uninstall -y
这将为所有的Mac,Windows和Linux系统正常工作。 为了让所有的PIP封装在requirements.txt文件列表(注:如果存在其他将创建新的这requirements.txt覆盖)
pip freeze > requirements.txt
现在,通过一个删除一个
pip uninstall -r requirements.txt
如果我们想在一次,然后删除所有
pip uninstall -r requirements.txt -y
如果你的工作,有一个现有项目requirements.txt
文件和环境已发散,只需更换requirements.txt
从上面的例子toberemoved.txt
。 然后,一旦你已经通过了步骤上面,您可以使用requirements.txt
更新您现在干净的环境。
而对于没有创建任何文件的单个命令(如joeb建议)。
pip uninstall -y -r <(pip freeze)
我想这样的作品与最新的
virtualenv --clear MYENV
我想提高这个答案了注释部分,因为它是在线程中最优雅的解决方案之一。 对于这个答案完全归功于@joeb 。
pip uninstall -y -r <(pip freeze)
这对我来说清除其中的许多上述答案不处理的virtualenv环境以外我的用户套餐文件夹的使用情况下,伟大的工作。
编辑:任何人都知道如何在一个Makefile这个命令?
我想补充这我的bash配置文件的方便:
alias pipuninstallall="pip uninstall -y -r <(pip freeze)"
然后运行:
pipuninstallall
如果你碰巧使用pipenv你可以运行:
pipenv uninstall --all
pip freeze
) pip freeze | xargs pip uninstall -y
pip list
) pip list | awk '{print $1}' | xargs pip uninstall -y
virtualenv
) virtualenv --clear MYENV
使用其他答案pip list
或pip freeze
必须包括--local
否则它也将卸载在共同命名空间中的软件包。
因此,这里是我经常使用的片段
pip freeze --local | xargs pip uninstall -y
要么
pip list --local | py -x "print(x.split()[0])" | xargs pip uninstall -y
详细了解此行为发出pip freeze --help
最快的方法是完全重新制作的virtualenv。 我假设你有一个相匹配的生产,如果不是requirements.txt文件:
# On production:
pip freeze > reqs.txt
# On your machine:
rm $VIRTUALENV_DIRECTORY
mkdir $VIRTUALENV_DIRECTORY
pip install -r reqs.txt
在Windows中,如果您的path
配置正确,你可以使用:
pip freeze > unins && pip uninstall -y -r unins && del unins
它应该是类Unix系统类似的案例:
pip freeze > unins && pip uninstall -y -r unins && rm unins
只是一个警告,这不完全是固体,你可能会碰到问题如“找不到文件”,但它可能在某些情况下仍然工作
编辑:为了清楚: unins
是此命令执行时,其具有写入到它的数据的任意的文件: pip freeze > unins
然后,它写在把这一文件是用来通过默许/事先批准卸载上述包pip uninstall -y -r unins
该文件完成后最终删除。
使用virtualenvwrapper功能:
wipeenv
见wipeenv文档
对于Windows用户来说,这是我在Windows PowerShell中使用
pip uninstall -y (pip freeze)
它的一个老问题,但我知道我没有穿过它绊倒所以以供将来参考现在你可以这样做:
pip uninstall [options] <package> ...
pip uninstall [options] -r <requirements file> ...
-r,--requirement文件
卸载在给定的要求,文件中列出的所有软件包。 这个选项可以多次使用。
从PIP文档版本8.1
这是我卸载所有Python包的最简单方法。
from pip import get_installed_distributions
from os import system
for i in get_installed_distributions():
system("pip3 uninstall {} -y -q".format(i.key))
首先,加入所有包requirements.txt
pip freeze > requirements.txt
然后删除所有
pip uninstall -y -r requirements.txt
跨平台支持仅使用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.
这是对我的作品的命令:
pip list | awk '{print $1}' | xargs pip uninstall -y
如果你正在运行virtualenv
:
virtualenv --clear </path/to/your/virtualenv>
例如,如果你的virtualenv是/Users/you/.virtualenvs/projectx
,那么你运行:
virtualenv --clear /Users/you/.virtualenvs/projectx
如果你不知道你的虚拟ENV所在,你可以运行which python
来获取路径从激活的虚拟包膜内
就我而言,我不小心安装了全球范围内使用自制安装许多包pip
在MacOS。 要还原到默认的包最简单的方法是简单的:
$ brew reinstall python
或者,如果你使用pip3
:
$ brew reinstall python3
在Windows中,命令的命令外壳
pip freeze | xargs pip uninstall -y
pip freeze | xargs pip uninstall -y
将无法正常工作。 因此,对于那些使用Windows的,我已经想通了另一种方式来做到这一点。
pip freeze
命令为.txt文件。 pip uninstall -r *textfile.txt*
如果您使用的pew
,你可以使用wipeenv命令:
pew wipeenv [env]
皮普无法知道包是通过它安装的是什么,并通过系统的软件包管理器进行安装的软件包的方式。 对于这一点,你需要做这样的事情
对于基于RPM的发行版(与您安装PIP你的Python版本替换python2.7):
find /usr/lib/python2.7/ |while read f; do
if ! rpm -qf "$f" &> /dev/null; then
echo "$f"
fi
done |xargs rm -fr
对于基于deb的分布:
find /usr/lib/python2.7/ |while read f; do
if ! dpkg-query -S "$f" &> /dev/null; then
echo "$f"
fi
done |xargs rm -fr
然后清理空目录遗留下来的:
find /usr/lib/python2.7 -type d -empty |xargs rm -fr
我发现最多的回答非常误导,因为它会从你的发行中删除所有(大多数?)Python包,可能给您留下一个破碎的系统。