在 Conda 环境中安装 PyTorch 的 .whl 包教程
本教程将指导你在 Conda 环境中通过手动下载和安装 PyTorch 的 .whl 文件来安装 PyTorch。这种方法适用于无法直接通过 conda install 或 pip install 获取 PyTorch 的场景(如离线环境或特定版本需求)。
whl链接汇总:
一、前提条件
- 已安装 Anaconda 或 Miniconda。
- 了解你的系统环境,包括:
- 操作系统(Windows、Linux 或 macOS)
- Python 版本(例如 3.8、3.9 等)
- 是否需要 GPU 支持(需要 CUDA 和 cuDNN 版本匹配)
- 确保有足够的磁盘空间存储
.whl文件和安装后的 PyTorch。
二、安装torch
1. 创建或激活 Conda 环境
首先,创建一个新的 Conda 环境或激活已有的环境,以确保 PyTorch 安装在一个隔离的环境中。
# 创建一个新环境(例如 Python 3.9)
conda create -n pytorch_env python=3.9
# 激活环境
conda activate pytorch_env
2. 确定所需的 PyTorch 版本
访问 PyTorch 官方网站(https://pytorch.org) PyTorch 的历史版本页面(https://pytorch.org/get-started/previous-versions/)查找适合你的系统配置的 .whl 文件。需要确认以下信息:
- PyTorch 版本(例如 2.0.1、1.13.1 等)
- Python 版本(例如 cp39 表示 Python 3.9)
- 操作系统(例如 linux_x86_64、win_amd64、macosx_10_9_x86_64 等)
- CUDA 版本(如果需要 GPU 支持,例如 cu118 表示 CUDA 11.8;若无 GPU,选择 cpu)
例如:
- CPU 版本:
torch-2.0.1-cp39-cp39-linux_x86_64.whl - GPU 版本(CUDA 11.8):
torch-2.0.1+cu118-cp39-cp39-linux_x86_64.whl
3. 下载 .whl 文件
从 PyTorch 官方提供的下载链接或历史版本页面下载对应的 .whl 文件。例如:
- 打开 https://download.pytorch.org/whl/torch/
- 根据你的系统和需求选择合适的
.whl文件,下载到本地(例如~/Downloads/torch-2.0.1-cp39-cp39-linux_x86_64.whl)。
注意:如果需要其他依赖(如
torchvision或torchaudio),也需要下载对应的.whl文件,并确保版本兼容。
4. 安装 .whl 文件
在激活的 Conda 环境中,使用 pip 安装下载的 .whl 文件。
# 确保已激活 Conda 环境
conda activate pytorch_env
# 使用 pip 安装 .whl 文件(替换为你的文件路径)
pip install ~/Downloads/torch-2.0.1-cp39-cp39-linux_x86_64.whl
提示:如果需要安装多个
.whl文件(例如torchvision),按依赖顺序逐个安装:pip install ~/Downloads/torchvision-0.15.2-cp39-cp39-linux_x86_64.whl
5. 验证安装
安装完成后,验证 PyTorch 是否正确安装并可用。
# 进入 Python 解释器
python
# 输入以下代码
import torch
print(torch.__version__) # 输出 PyTorch 版本
print(torch.cuda.is_available()) # 检查是否支持 GPU(返回 True 表示可用)
示例输出:
2.0.1
True # 如果支持 GPU
6. 解决常见问题
-
版本不匹配:确保 Python 版本、CUDA 版本和
.whl文件匹配。例如,cp39对应 Python 3.9,cu118对应 CUDA 11.8。 -
缺少依赖
:PyTorch 可能需要其他依赖(如
numpy)。如果报错,尝试安装:
pip install numpy -
GPU 支持问题:如果
torch.cuda.is_available()返回False,检查 CUDA 和 cuDNN 是否正确安装,或者确认是否下载了 CPU 版本的.whl文件。 -
离线安装:如果在离线环境中,确保所有依赖的
.whl文件都已下载并按顺序安装。
7. (可选)安装其他 PyTorch 生态库
如果你需要 torchvision、torchaudio 或其他相关库,重复步骤 3 和 4,下载并安装对应的 .whl 文件。例如:
pip install ~/Downloads/torchvision-0.15.2-cp39-cp39-linux_x86_64.whl
三、安装torch 的对应 torchvision 版本
| Torch 版本 | TorchVision 版本 | Torchaudio 版本 | 支持的 Python 版本 | 支持的 CUDA 版本 |
|---|---|---|---|---|
| 2.5.1 | 0.20.1 | 2.5.1 | >=3.9, < 3.13(3.12)[9/10/11/12] | 12.4 / 12.1 / 11.8 |
| 2.5.0 | 0.20.0 | 2.5.0 | >=3.9, < 3.13(3.12) | 12.4 / 12.1 / 11.8 |
| 2.4.1 | 0.19.1 | 2.4.1 | >=3.8, < 3.13(3.12)[8/9/10/11/12] | 12.4 / 12.1 / 11.8 |
| 2.4.0 | 0.19.0 | 2.4.0 | >=3.8, < 3.13(3.12) | 12.4 / 12.1 / 11.8 |
| 2.3.1 | 0.18.1 | 2.3.1 | >=3.8, < 3.13(3.12)[8/9/10/11/12] | 12.1 / 11.8 |
| 2.3.0 | 0.18.0 | 2.3.0 | >=3.8, < 3.13(3.12) | 12.1 / 11.8 |
| 2.2.2 | 0.17.2 | 2.2.2 | >=3.8, < 3.12 [8/9/10/11] | 12.1 / 11.8 |
| 2.2.1 | 0.17.1 | 2.2.1 | >=3.8, < 3.12 | 12.1 / 11.8 |
| 2.2.0 | 0.17.0 | 2.2.0 | >=3.8, < 3.12 | 12.1 / 11.8 |
| 2.1.2 | 0.16.2 | 2.1.2 | >=3.8, < 3.12(3.10)[8/9/10/11] | 12.1 / 11.8 |
| 2.1.1 | 0.16.1 | 2.1.1 | >=3.8, < 3.12(3.10) | 12.1 / 11.8 |
| 2.1.0 | 0.16.0 | 2.1.0 | >=3.8, < 3.12(3.10) | 12.1 / 11.8 |
| 2.0.0 | 0.15.0 | 2.0.0 | >=3.8, < 3.12(3.8)[8/9/10/11] | 11.8 / 11.7 |
| 1.13.1 | 0.14.1 | 0.13.1 | >=3.7.2, <=3.10(3.8)[7/8/9/10] | 11.7 / 11.6 |
| 1.13.0 | 0.14.0 | 0.13.0 | >=3.7.2, <=3.10(3.8) | 11.7 / 11.6 |
| 1.12.1 | 0.13.1 | 1.12.1 | >=3.7, <=3.10(3.8)[7/8/9/10] | 11.6 / 11.3 / 10.2 |
| 1.12.0 | 0.13.0 | 1.12.0 | >=3.7, <=3.10(3.8) | 11.6 / 11.3 / 10.2 |
| 1.11.0 | 0.12.0 | 1.11.0 | >=3.7, <=3.10(3.8) | 11.3 / 10.2 |
| 1.10.1 | 0.11.2 | 0.10.1 | >=3.6, <=3.9(3.8)[6/7/8/9] | 11.3 / 10.2 |
| 1.10.0 | 0.11.0 | 0.10.0 | >=3.6, <=3.9(3.8) | 11.3 / 10.2 |
| 1.9.1 | 0.10.1 | 0.9.1 | >=3.6, <=3.9(3.8)[6/7/8/9] | 11.1 / 10.2 |
| 1.9.0 | 0.10.0 | 0.9.0 | >=3.6, <=3.9(3.8) | 11.1 / 10.2 |
| 1.8.1 | 0.9.1 | 0.8.1 | >=3.6, <=3.9(3.8)[6/7/8/9] | 11.1 / 10.2 |
| 1.8.0 | 0.9.0 | 0.8.0 | >=3.6, <=3.9(3.8) | 11.1 / 10.2 |
| 1.7.1 | 0.8.2 | 0.7.2 | >=3.6(3.6) | 11.0 / 10.2 / 10.1 |
| 1.7.0 | 0.8.0 | 0.7.0 | >=3.6(3.6) | 11.0 / 10.2 / 10.1 |
1.访问 PyTorch 官网:
- 打开浏览器,访问 PyTorch 官方安装页面:https://pytorch.org/get-started/locally/
2.使用“Get Started”安装向导:
- 在页面上,你会看到一个交互式的安装命令生成器,标题为“Start Locally”。
- 根据你的系统配置选择以下选项:
- PyTorch Build:选择
Stable(稳定版)或Preview (Nightly)(预览版)。 - Your OS:选择你的操作系统(例如 Linux、Windows、MacOS)。
- Package:选择包管理器(例如
pip或conda)。 - Language:选择 Python 版本(你的环境是 Python 3.10)。
- Compute Platform:选择计算平台(例如 CUDA 12.1,与你的
torch==2.5.1+cu121匹配,或 CPU)。
- PyTorch Build:选择
3.查看生成的安装命令:
-
选择完选项后,页面会生成一个安装命令,例如:
pip install torch==2.5.1 torchvision==0.20.1 torchaudio==2.5.1 --index-url https://download.pytorch.org/whl/cu121 -
这个命令会明确列出与
torch==2.5.1兼容的torchvision版本(例如torchvision==0.20.1)。
4.确认 torch 和 torchvision 版本对应关系:
- 根据你的
environment.yml文件,torch==2.5.1+cu121已经安装。官网显示torch==2.5.1对应的torchvision版本是0.20.1(与 CUDA 12.1 兼容)。https://pytorch.org/get-started/previous-versions/ - 如果你选择了不同的 CUDA 版本或其他平台(如 CPU),官网会列出相应的
torchvision版本。
5.查看历史版本(可选):
-
如果需要查找其他
torch版本的对应torchvision版本,滚动到页面底部的“Previous PyTorch Versions”部分。 -
点击链接(例如 https://pytorch.org/get-started/previous-versions/),会列出历史版本的安装命令,明确显示每个
torch版本对应的torchvision版本。例如:# CUDA 12.1 pip install torch==2.5.1 torchvision==0.20.1 torchaudio==2.5.1 --index-url https://download.pytorch.org/whl/cu121
6.直接访问 torchvision GitHub(高级用户):
- 如果官网信息不够详细,可以访问 torchvision 的 GitHub 仓库:https://github.com/pytorch/vision
- 在
README或RELEASE.md文件中,会有版本兼容性表格,列出torch和torchvision的对应关系。 - 例如,
vision/RELEASE.md可能提到torch==2.5.1对应torchvision==0.20.1。
四、验证安装
-
environment.yml显示torch==2.5.1+cu121,Python 3.10,CUDA 12.1。 -
根据 PyTorch 官网,
torch==2.5.1+cu121对应的torchvision版本是0.20.1+cu121。 -
安装命令:
pip install torchvision==0.20.1 --index-url https://download.pytorch.org/whl/cu121
安装 torchvision==0.20.1 后,运行以下代码确认:
import torch
import torchvision
print(torch.__version__) # 应输出 2.5.1+cu121
print(torchvision.__version__) # 应输出 0.20.1+cu121
五、注意事项
-
Conda 与 pip 混合使用:在 Conda 环境中使用
pip安装.whl文件是安全的,但避免在同一环境中混用conda install和pip install安装 PyTorch,以免出现依赖冲突。 -
存储空间:PyTorch 的
.whl文件和安装后的库可能占用较大空间(GPU 版本可能超过 1GB)。 -
网络问题:如果无法访问 PyTorch 官网,可尝试从镜像源(如 https://mirrors.tuna.tsinghua.edu.cn/pytorch/)下载
.whl文件。 -
CUDA 匹配:确保
torchvision的 CUDA 版本(+cu121)与torch一致,否则可能导致不兼容。 -
PyPI 索引:使用
--index-url https://download.pytorch.org/whl/cu121确保从 PyTorch 官方索引下载,普通 PyPI 可能缺少 CUDA 版本。 -
Python 版本: Python 3.10 与
torch==2.5.1和torchvision==0.20.1兼容,无需调整。

506

被折叠的 条评论
为什么被折叠?



