在 CentOS 上“导入 PyTorch”通常分两步:安装 PyTorch 和 在 Python 中 import。下面按常见情况说明。
cat /etc/centos-release
python3 --version
pip3 --version
如果没有 Python3:
sudo yum install -y python3 python3-pip
pip3 install torch torchvision torchaudio
先确认 CUDA 版本:
nvidia-smi
例如 CUDA 11.8:
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
进入 Python:
python3
然后输入:
import torch
print(torch.__version__)
如果没有报错,说明导入成功 ✅
sudo yum install -y libgomp
建议:
conda install pytorch torchvision torchaudio cpuonly -c pytorch
pip3 install --upgrade pip
# 使用 conda 最稳
conda create -n torch python=3.9
conda activate torch
conda install pytorch torchvision torchaudio cpuonly -c pytorch
python
>>> import torch
如果你能告诉我:
我可以给你精确的一条命令安装方案。