批量卸载所有 pip 包

pip list 然后uninstall 全部list

一行命令即可:先列出所有用 pip 安装的包,再全部卸载。

CMD(Windows)

for /f "skip=2 delims= " %i in ('pip list --format=freeze') do pip uninstall -y %i

PowerShell(Windows)

pip list --format=freeze | %{ $_.split('=')[0] } | pip uninstall -y -r -

Bash(Linux / macOS / WSL)

pip uninstall -y $(pip list --format=freeze | cut -d= -f1)

执行前记得先 conda activate 环境名source venv/bin/activate,确保卸载的是目标虚拟环境里的包。

步骤 2:查看当前通过 pip 安装的包

bash浅色版本

pip list

步骤 3:导出包名(只保留包名,去掉版本号)

bash浅色版本

pip freeze | cut -d'=' -f1

Windows 用户可以用:

cmd浅色版本

pip freeze | findstr /R /C:"=="

然后手动删除 ==版本号

步骤 4:批量卸载所有 pip 包

bash浅色版本

pip freeze | cut -d'=' -f1 | xargs pip uninstall -y

Windows 用户可以用 PowerShell:

powershell浅色版本

pip freeze | % { $_.split('==')[0] } | % { pip uninstall $_ -y }

排错

Could not install packages due to an OSError

ERROR: Could not install packages due to an OSError: [Errno 2] No such file or directory: 'e:\pycharm\anaconda\envs\test\lib\site-packages\numpy-1.24.4.dist-info\METADATA'

原因
numpy-1.24.4.dist-info 目录里的 METADATA 文件被误删/损坏,导致 pip 无法读取包的元数据,于是任何后续安装/卸载操作都会报
[Errno 2] No such file or directory: ...METADATA

解决步骤(任选其一)

  1. 用 pip 的 --force-reinstall 把坏包“盖”过去

    conda activate test          # 进入目标环境
    pip install --force-reinstall --no-cache-dir numpy==1.24.4
    

    这会让 pip 重新下载并覆盖整个 numpy-1.24.4.dist-info 目录,METADATA 就回来了。

  2. 如果 1 失败,先手动删掉残留目录再装

    conda activate test
    rmdir /s /q "e:\pycharm\anaconda\envs\test\lib\site-packages\numpy-1.24.4.dist-info"
    pip install numpy==1.24.4
    
  3. 环境已乱,干脆重建

    conda deactivate
    conda remove -n test --all -y
    conda create -n test python=3.10 numpy=1.24.4
    

通常方案 1 就能解决;若仍报错,就按 2 → 3 的顺序处理即可。

pip install kiwisolver