前言

pytorch的安装倒挺顺利,但是torch-geometric一直报错……最后还是在学姐的经验帮助下安装好了,torch-geometric安装对于版本的匹配要求极其严格,另外建议使用conda创建环境

准备

如果您是国内用户,那建议首先更换源为国内源

添加清华源

打开anaconda prompt,输入:

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

配置好后进行查看

conda config --show-sources

删除安装源

conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/

换回默认源

conda config --remove-key channels

使用conda创建环境

conda create -n (此处填写环境名字) python=3.8

如果您不确定安装哪一个python版本,建议您按示例python版本,否则极大可能出现不适配的情况

使用conda退出环境

conda deactivate (此处填写环境名字)

使用conda激活环境

conda activate (此处填写环境名字)

使用conda删除环境

conda remove -n (此处填写环境名字) --all

使用conda删除环境中的包

conda remove --name (此处填写环境名字) (此处填写包的名字)
### 其他conda常用指令后续补充
……
## 查看环境是否安装成功
```bash
conda info --envs</code></pre>
<p><img src="https://evariste-xu.oss-cn-beijing.aliyuncs.com/20210621210102.png" alt="conda环境" />
前面有*标识的代表用户目前所处环境,通过上述激活环境指令进行环境切换</p>
<pre><code>## 安装pytorch
[pytorch官网](https://pytorch.org/)
网上许多教程都在最后加上了-c pytorch,也就是官方的安装命令。但加上后意思是从官方下载安装,这样前面配置的国内源就没有用了,所以一定要去掉这两个参数
下面给出的示例是仅cpu不用gpu加速的版本
```bash
conda install pytorch==1.8.0 torchvision==0.9.0 torchaudio==0.8.0 cpuonly

如果安装没有问题的话,可以进入python交互界面输入命令进行验证:

import torch
print(torch.__version__)

到此pytorch就安装好了

安装torch-geometric

使用指定匹配版本(torch-1.8.0)安装,同样是仅cpu版本

pip install torch-scatter -f https://pytorch-geometric.com/whl/torch-1.8.0+cpu.html
pip install torch-sparse -f https://pytorch-geometric.com/whl/torch-1.8.0+cpu.html
pip install torch-cluster -f https://pytorch-geometric.com/whl/torch-1.8.0+cpu.html
pip install torch-spline-conv -f https://pytorch-geometric.com/whl/torch-1.8.0+cpu.html
pip install torch-geometric

如果不出意外,至此仅cpu版本的环境就安装完成了

如果您不幸遇到了error,打算重新配置,可以使用如下命令卸载失败环境

pip uninstall torch-scatter
pip uninstall torch-sparse
pip uninstall torch-cluster
pip uninstall torch-spline-conv
pip uninstall torch-geometric
conda uninstall pytorch
conda uninstall torchvision
conda uninstall torchaudio

随心所至