我正在尝试修复我的一个virtualenv -我想将所有已安装的库重置为与生产相匹配的库。
有没有一种快速简单的方法来处理皮普?
我正在尝试修复我的一个virtualenv -我想将所有已安装的库重置为与生产相匹配的库。
有没有一种快速简单的方法来处理皮普?
当前回答
在Windows的命令Shell中,pip freeze | xargs pip uninstall -y命令不起作用。所以对于那些使用Windows的人,我想出了另一种方法。
将pip freeze命令中所有已安装的pip包的名称复制到一个.txt文件中。 然后,找到。txt文件所在的位置,运行pip uninstall -r *textfile.txt*命令
其他回答
这适用于最新版本。我认为这是最简洁明了的表达方式。
virtualenv --clear MYENV
但是为什么不直接删除并重新创建virtualenv呢?
不变性规则。此外,很难记住所有的管道和其他解决方案使用。
仅使用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 uninstall [options] <package> ...
pip uninstall [options] -r <requirements file> ...
-r,——需求文件 卸载给定需求文件中列出的所有包。此选项可多次使用。
来自PIP文档8.1版
如果你正在运行virtualenv:
virtualenv --clear </path/to/your/virtualenv>
例如,如果virtualenv是/Users/you/。Virtualenvs /projectx,然后运行:
virtualenv --clear /Users/you/.virtualenvs/projectx
如果你不知道你的虚拟环境的位置,你可以在激活的虚拟环境中运行python来获取路径
(添加这个作为答案,因为我没有足够的声誉来评论@blueberryfields的回答)
@blueberryfields的答案工作得很好,但如果没有包可以卸载(如果这个“卸载全部”是脚本或makefile的一部分,这可能是一个问题)。当使用GNU版本的xargs时,这个问题可以用xargs -r来解决:
pip freeze --exclude-editable | xargs -r pip uninstall -y
来自man xargs:
- r, no-run-if-empty 如果标准输入中不包含非空字符,请不要执行该命令。通常情况下,该命令即使存在也只运行一次 没有输入。这个选项是一个GNU扩展。