在 Linux 上把 PyTorch 彻底卸载干净,通常分三步:卸载 pip / conda 包 → 清理残留文件 → 检查环境变量。下面按常见情况给你说明。
which python
python -V
pip list | grep torch
conda list | grep torch
pip 结果 → 用 pip 卸载conda 结果 → 用 conda 卸载pip uninstall torch torchvision torchaudio -y
如果装过 CPU / CUDA 版本:
pip uninstall torch torchvision torchaudio functorch -y
conda uninstall pytorch torchvision torchaudio -y
如果你是用官方命令装的:
conda uninstall pytorch torchvision torchaudio pytorch-cuda -y
rm -rf ~/.cache/pip
rm -rf ~/.torch
rm -rf ~/.cache/torch
python -c "import torch"
如果还提示存在:
pip show torch
找到 Location,手动删目录。
echo $LD_LIBRARY_PATH
echo $PYTHONPATH
如果里面有:
/usr/local/cudatorch在 ~/.bashrc 或 ~/.zshrc 中删除对应行,然后:
source ~/.bashrc
python -c "import torch"
✅ 报 ModuleNotFoundError 就成功了
再查一次:
pip list | grep torch
conda list | grep torch
pip install torch torchvision torchaudio
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
如果你愿意,可以贴一下:
python -m torch.utils.collect_env我可以帮你精确清理,避免误删。