我正在尝试修复我的一个virtualenv -我想将所有已安装的库重置为与生产相匹配的库。
有没有一种快速简单的方法来处理皮普?
我正在尝试修复我的一个virtualenv -我想将所有已安装的库重置为与生产相匹配的库。
有没有一种快速简单的方法来处理皮普?
当前回答
这是对我有用的命令:
pip list | awk '{print $1}' | xargs pip uninstall -y
其他回答
这在Windows上很管用:
pip uninstall -y (pip freeze)
(添加这个作为答案,因为我没有足够的声誉来评论@blueberryfields的回答)
@blueberryfields的答案工作得很好,但如果没有包可以卸载(如果这个“卸载全部”是脚本或makefile的一部分,这可能是一个问题)。当使用GNU版本的xargs时,这个问题可以用xargs -r来解决:
pip freeze --exclude-editable | xargs -r pip uninstall -y
来自man xargs:
- r, no-run-if-empty 如果标准输入中不包含非空字符,请不要执行该命令。通常情况下,该命令即使存在也只运行一次 没有输入。这个选项是一个GNU扩展。
从虚拟环境中删除所有包的最佳方法。
Windows PowerShell:
pip freeze > unins ; pip uninstall -y -r unins ; del unins
Windows命令提示符:
pip freeze > unins && pip uninstall -y -r unins && del unins
Linux:
pip3 freeze > unins ; pip3 uninstall -y -r unins ; rm unins
这是我卸载所有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))
最快的方法是完全重做virtualenv。我假设你有一个符合生产的requirements.txt文件,如果没有的话:
# On production:
pip freeze > reqs.txt
# On your machine:
rm $VIRTUALENV_DIRECTORY
mkdir $VIRTUALENV_DIRECTORY
pip install -r reqs.txt