Anaconda 安装 PyTorch 和 DGL

2023-07-29,,

安装 PyTorch

Anaconda 是 PyTorch 官方推荐的包管理工具,它会帮助安装所有的依赖项。当使用 conda 安装的时候,可能会出现下载过慢的问题,需要更换清华源来代替默认的conda源。

1、添加清华源,注意是 http ,不是 https

conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/clound/pytorch # 从 channel 中安装包时显示 channel 的 url, 方便查看包的安装来源(如下图)
conda config --set show_channel_urls yes

2、安装 PyTorch

PyTorch官网选择对应的参数得到下载命令。

# Run this Command, 去掉 -c pytorch, 否则还是从默认源下载
conda install pytorch torchvision torchaudio cpuonly

3、补充说明

在安装过程中如果遇到 EnvironmentNotWritableError: The current user does not have write permissions to the target environment. ,使用管理员身份运行 Anaconda Powershell Prompt 即可。

4、PyTorch安装成功示例

(base) PS C:\Windows\system32> python
Python 3.9.7 (default, Sep 16 2021, 16:59:28) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>>

安装 DGL

1、安装DGL之前务必请先安装PyTorch

2、DGL官网选择对应参数得到下载命令

3、使用管理员身份运行 Anaconda Powershell Prompt ,执行命令 conda install -c dglteam dgl

4、设置 ~/.dgl/config.json 文件的 backend 默认值设为 pytorch

# DGL 是基于多种深度学习框架搭建的, 运行时需要基于这些深度学习框架
# BACKEND 有三个选择 pytorch, mxnet, tensorflow
python -m dgl.backend.set_default_backend [BACKEND]

5、尝试在 Python 中导入 dgl

(base) PS C:\Windows\system32> PYTHON
Python 3.9.7 (default, Sep 16 2021, 16:59:28) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information. >>> import dgl
>>>

至此,PyTorch 和 DGL 就安装完成啦!

Anaconda 安装 PyTorch 和 DGL的相关教程结束。

《Anaconda 安装 PyTorch 和 DGL.doc》

下载本文的Word格式文档,以方便收藏与打印。