一、找到合适的安装方式
pytorch官网:https://pytorch.org/
二、安装命令
pip3 install torch torchvision -i https://pypi.mirrors.ustc.edu.cn/simple
后面的是镜像,当然也可以采用其他源,网上很多;
国内镜像源列表
豆瓣(douban) http://pypi.douban.com/simple/
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
阿里云 http://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/
报错说明:
1. 报错
pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='mirrors.tuna.tsinghua.edu.cn', port=443): Read timed out.
增加 --default-timeout=1000
命令修改为:
pip3 install --default-timeout=1000 torch torchvision -i https://pypi.mirrors.ustc.edu.cn/simple
2. 报错
ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/usr/local/lib/python3.5/dist-packages/numpy-1.18.2.dist-info'
Consider using the `--user` option or check the permissions.
增加--user
命令修改为:
pip3 install --default-timeout=1000 torch torchvision -i https://pypi.mirrors.ustc.edu.cn/simple --user
3. 报错
ERROR: THESE PACKAGES DO NOT MATCH THE HASHES FROM THE REQUIREMENTS FILE. If you have updated the package versions, please update the hashes. Otherwise, examine the package contents carefully; someone may have tampered with them.
增加 --upgrade
命令修改为:
pip3 install --default-timeout=1000 --upgrade torch torchvision -i https://pypi.mirrors.ustc.edu.cn/simple --user
4. 安装结果
如图
三、安装测试
测试pytorch是否安装成功
在终端输入
python3(注意是输入python3,如果只输入python,会默认是python2,当然,之后你也可以自己更改python的默认版本)
import torch
import torchvision