-
Virtualenv 安装
1. 安装virtualenv
pip install virtualenv
2. virturalenv 创建虚拟环境
virtualenv py_pytorch
# py_pytorch 为虚拟环境的名字
# 默认保存在当前路径
3. 激活虚拟环境
# 切换到创建环境时的路径!
souce py_pytorch/bin/activate
# 查看当前路径下的文件,可以看到当前路径下有没有自己创建的虚拟环境
ls
# 如果是创建在根目录下,要注意区分:
1. [xxx@localhost ~]$
2. [xxx@localhost /]$
4. torch 安装
# 进入官网,选择与自己 cuda 版本相匹配的版本
# https://pytorch.org/
# CUDA 10.1
pip install torch==1.4.0 torchvision==0.5.0
5. 检查是否安装成功
import torch
# 不报错及成功
# 若安装的 torch 为 GPU 版,还应当输入
torch.cuda.is_available()
# 若输出为 True 及成功
6. 退出虚拟环境
deactivate
注:用 pip 命令安装各种需要的包
-
Conda 安装
1. 安装 Anaconda ,具体步骤 CSDN 可查
2. 创建虚拟环境
conda create --name py_torch python=3.6
# py_torch 为虚拟环境名称,可指定 python 版本
# 可以使用下面的命令查看虚拟环境是否创建成功
conda info --envs
3. 激活虚拟环境
source activate py_torch
4. torch 安装
- 失败案例 1
# CUDA 10.1
conda install pytorch==1.4.0 torchvision==0.5.0 cudatoolkit=10.1 -c pytorch
# 报 HttpError 错误
- 失败案例 2:
# 更改镜像源
# 1. 清华镜像源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
conda config --set show_channel_urls yes # 显示安装时使用的路径
# Traceback
# channel url:https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
# error code:404
清华镜像源失效,更改镜像源
尝试改错方法
-
删除 default 行
# 1. 删除 .condarc 文件中的 default
# 打开 condarc
vim ~/.condarc
# 输入 i ,进入编辑模式,删掉 default 一行,esc,输入“:”,wq 退出
# 无果
-
将 https 更改为 http,无果
-
将安装命令中的 -c pytorch 删除后再安装也无果... ...
如果更改过 .condarc 文件后,再使用 conda 安装时,遇到以下错误:
ConfigurationLoadError:Unable to load configuration file
path:/home/xxx/.condarc
reason:invalid yaml at line 6,column 0
解决办法(应该不唯一):
rm -rf .condarc # 暴力删除 .condarc ,重新配置文件及镜像源
conda config # 自动重新生成 .condarc 文件
# 然后重新配置 .condarc 文件中镜像源
- 失败案例 3:
# 更改镜像源前需将之前路径删掉
# 恢复默认镜像源
conda config --remove-key channels
# 删除某条路径
conda config --remove channels https:// ...
将清华镜像源更改为中科大镜像源
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/menpo/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/
# 报错
# CondaHTTPError:HTTP 000 CONNECTION FAILED for url <https://mirrors/ustc.edu.cn/anaconda/cloud/linux-64/current_repodata.json>
尝试删除 default 行和将 https 更改为 http,均无果
- 成功案例:使用豆瓣镜像源
第一次是使用 conda config --add 命令将豆瓣镜像源添加到 .condarc 文件中,安装时还是报了 404 的错误
第二次,参考 https://blog.csdn.net/weixin_41804998/article/details/107512700,成功安装,速度很快! (可以更换版本)
pip install torch==1.5.1 torchvision==0.6.1 -f https://download.pytorch.org/whl/torch_stable/html -i https://pypi.douban.com/simple
5. 检查是否安装成功,与上面的方法一样
6. 退出虚拟环境
conda deactivate