1、安装anaconda
点击前往anaconda官网:www.anaconda.com
提示:建议下载最新版本的anaconda,不然容易出现如下报错:CondaError: Cannot link a source that does not exist. C:\Anaconda3\Scripts\conda.exe,之后可能还要再重新下载anaconda。
2、修改到清华源
在c盘/用户下找个.condarc这个文件,将里面的内容修改到如下:
channels:
- defaults
show_channel_urls: true
channel_alias: https://mirrors.tuna.tsinghua.edu.cn/anaconda
default_channels:
- http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
- http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
- http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro
- http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
conda-forge: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
msys2: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
bioconda: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
menpo: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
提示:这里所有的源里都是http而不是https,如果引入https会报错
3、设置超时时间
使用如下代码设置超时时间,这一点很重要,就算换成国内的清华源下载有时依旧非常缓慢,设置长时间的超时时间能让下载pytorch时系统不会自动断开下载。
pip --default-timeout=1000 install requests
4、去pytorch官网查找下载指令
选择对应版本(cuda要提前自己下载,非常简单)的语句,用管理员权限打开cmd,输入命令开始下载。
5、测试是否成功
输入python。1、输入import torch,如果输入后没有任何提示,就是成功了。2、然后再输入torch.cuda.is_available(),返回的是True,就是成功了。
6、可能遇到的问题
1、failed with initial frozen solve. Retrying with flexible solve
解决:去http://anaconda.org这个网站搜索你想要的包,这里就是搜索pytorch进行下载。
2、ERROR conda.core.link:_execute(502): An error occurred while uninstalling package 'defaults::numpy-base-1.15.1-py37h8128ebf_0'.
PermissionError(13, '拒绝访问。')
这里就是未用管理员权限访问。
3、import torch时出现如下问题
一般出现此问题原因可能有两个,一个是numpy的版本和python的版本不适配(python3.9和numpy1.19.4不适配),另一个就是安装numpy时候使用pip安全,而不是conda install numpy
解决:卸载当前numpy,代码如下:
pip uninstall numpy==1.21.5
再安装对应版本,如果你不是1.19.4就安装自己当前版本,如果是的就安装numpy1.19.3,代码如下:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn numpy==1.19.3