我正在尝试修复我的一个virtualenv -我想将所有已安装的库重置为与生产相匹配的库。
有没有一种快速简单的方法来处理皮普?
我正在尝试修复我的一个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.
其他回答
就我而言,我意外地在macOS上使用homebrew安装的pip在全球范围内安装了许多软件包。恢复到默认包的最简单方法是:
$ brew reinstall python
或者,如果你使用pip3:
$ brew reinstall python3
Pip无法知道它安装了哪些包,系统的包管理器安装了哪些包。为此,您需要这样做
对于基于rpm的发行版(将python2.7替换为安装PIP的python版本):
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包,可能会给你留下一个坏掉的系统。
方法一(冷冻)
pip freeze | xargs pip uninstall -y
方法二(附pip表)
pip list | awk '{print $1}' | xargs pip uninstall -y
方法三(使用virtualenv)
virtualenv --clear MYENV
pip3 freeze --local | xargs pip3 uninstall -y
情况可能是必须多次运行此命令才能获得空pip3 freeze—local。
我找到了这个片段作为替代解决方案。这是一个比重做virtualenv更优雅的删除库:
pip freeze | xargs pip uninstall -y
如果你有通过VCS安装的包,你需要排除这些行并手动删除包(从下面的注释中提升):
pip freeze | grep -v "^-e" | xargs pip uninstall -y
如果你有直接从github/gitlab安装的包,这些包将有@。 如:
django @ git+https://github.com/django.git@<sha>
您可以添加cut -d "@" -f1来获得卸载所需的包名。
pip freeze | cut -d "@" -f1 | xargs pip uninstall -y