温馨提示×

linux pytorch怎么卸载干净

小樊
48
2026-09-25 17:37:58
栏目: 智能运维

在 Linux 上把 PyTorch 彻底卸载干净,通常分三步:卸载 pip / conda 包 → 清理残留文件 → 检查环境变量。下面按常见情况给你说明。


一、先确认你是怎么装的

which python
python -V

pip list | grep torch
conda list | grep torch
  • 有 pip 结果 → 用 pip 卸载
  • 有 conda 结果 → 用 conda 卸载
  • 两者都有 → 两个都卸

二、卸载 PyTorch

✅ 如果是 pip 安装的

pip uninstall torch torchvision torchaudio -y

如果装过 CPU / CUDA 版本:

pip uninstall torch torchvision torchaudio functorch -y

✅ 如果是 conda 安装的

conda uninstall pytorch torchvision torchaudio -y

如果你是用官方命令装的:

conda uninstall pytorch torchvision torchaudio pytorch-cuda -y

三、清理残留文件(很重要)

1️⃣ 删除 pip 缓存

rm -rf ~/.cache/pip

2️⃣ 删除 torch 残留目录

rm -rf ~/.torch
rm -rf ~/.cache/torch

3️⃣ 检查 Python 包目录

python -c "import torch"

如果还提示存在:

pip show torch

找到 Location,手动删目录。


四、检查环境变量

echo $LD_LIBRARY_PATH
echo $PYTHONPATH

如果里面有:

  • /usr/local/cuda
  • torch
  • anaconda 的 torch 路径

在 ~/.bashrc 或 ~/.zshrc 中删除对应行,然后:

source ~/.bashrc

五、确认是否干净

python -c "import torch"

✅ 报 ModuleNotFoundError 就成功了

再查一次:

pip list | grep torch
conda list | grep torch

六、如果你要重装(可选)

pip(CPU)

pip install torch torchvision torchaudio

pip(CUDA 11.8 示例)

pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118

conda

conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia

如果你愿意,可以贴一下:

  • 系统(Ubuntu / CentOS)
  • 安装方式(pip / conda)
  • python -m torch.utils.collect_env

我可以帮你精确清理,避免误删。

0 踩