Windows 系统如何彻底卸载所有 pip 安装的包
在 Windows 系统上卸载所有通过 pip 安装的包有多种方法,我将详细介绍每种方法及其适用场景。
方法一:使用 pip freeze 和 pip uninstall(推荐)
这是最常用和最安全的方法,可以批量卸载所有第三方包。
步骤 1:生成已安装包列表
pip freeze > requirements.txt
步骤 2:批量卸载所有包
pip uninstall -r requirements.txt -y
参数解释:
-r requirements.txt:从文件读取要卸载的包列表-y:自动确认卸载,无需手动确认每个包
步骤 3:清理临时文件
del requirements.txt
方法二:通过 Python 脚本批量卸载
如果你有特殊需求或需要更多控制,可以使用 Python 脚本:
import subprocess
import sys
def uninstall_all_packages():
# 获取所有已安装的包
installed = subprocess.check_output([sys.executable, '-m', 'pip', 'freeze'])
packages = [pkg.decode().split('==')[0] for pkg in installed.split()]
# 过滤掉 pip、setuptools、wheel 等基础包
system_packages = {'pip', 'setuptools', 'wheel', 'distribute'}
to_uninstall = [pkg for pkg in packages if pkg.lower() not in system_packages]
if not to_uninstall:
print("没有可卸载的第三方包")
return
print()
pkg to_uninstall:
()
confirm = ()
confirm.lower() != :
()
pkg to_uninstall:
:
subprocess.check_call([sys.executable, , , , pkg, , ])
()
subprocess.CalledProcessError:
()
()
__name__ == :
uninstall_all_packages()


