Python入门之anaconda安装pytorch环境快速上手(超详细!!!)

前言

在学习python时,一般都会使用Anaconda。

Anaconda是一个强大的开源数据科学平台,它将很多好的工具整合在一起,极大地简化了使用者的工作流程,并能够帮助使用者解决一系列数据科学难题。

有小伙伴纠结先安装python还是安装anaconda,这边的建议是装anaconda,就不需要单独装python了,因为anaconda自带python,且安装了anaconda之后,默认python版本是anaconda自带的python版本。

一、安装包下载

1)官网下载地址:https://www.anaconda.com/download

请添加图片描述

2)其他版本地址:https://repo.anaconda.com/archive/,找到想要的版本以及适合的系统即可

在这里插入图片描述

3)嫌麻烦的朋友,安装包已备好,Win系统64位版本
在这里插入图片描述

二、详细安装步骤

1、双击运行安装程序,点击Next

在这里插入图片描述

2、避免安装在C盘(会导致电脑变慢/卡)

在这里插入图片描述

3、勾选两个框,设置环境变量以及设为默认Python启动程序,点击 Install

在这里插入图片描述

4、耐心等待安装完毕

在这里插入图片描述

5、直接点击 Next

在这里插入图片描述

6、两个勾去掉,避免打开网页还得关闭,直接 Finish 安装结束

在这里插入图片描述

7、测试
Anaconda 安装结束后,会新增以下几个应用:

在这里插入图片描述

双击 Anaconda Prompt 输入如下指令测试:

conda --version

若输出以下,即为安装成功:

conda 23.1.0

7.1 更改镜像源

当我们使用 Conda 下载并安装 Python 包时,默认访问国外服务器,速度较慢或报错,所以需要换源。

更改 Conda 默认源有两种方式:(1)config 指令;(2)修改 .condarac 文件。

(1)config

Anaconda Prompt 中输入如下指令添加清华镜像源:

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes

此时,打开C盘用户目录(即 C:\Users\xxx,xxx为你的用户名),会发现多了一个 .condarc 文件,将其用记事本打开如下所示:

在这里插入图片描述
其他操作:

  • 重置/删除镜像源(或直接去 .condarc 文件中全选删除):
conda config --remove-key channels
  • 如果镜像源中含有 defaults 需要删掉,这样在下载时会默认使用清华镜像源:
conda config --remove channels defaults

(2).condarc

用记事本打开 .condarc 文件,并添加以下内容:

channels:
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
  - http://mirrors.ustc.edu.cn/anaconda/cloud/msys2/
  - http://mirrors.ustc.edu.cn/anaconda/pkgs/free/
  - http://mirrors.ustc.edu.cn/anaconda/pkgs/main/
  - http://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
  - http://mirrors.ustc.edu.cn/anaconda/cloud/menpo/
  - http://mirrors.ustc.edu.cn/anaconda/cloud/bioconda/
ssl_verify: true
show_channel_urls: true

三、pytorch的安装

进入到anaconda prompt

在这里插入图片描述
执行代码

conda env list

执行完代码你可以看到你在anaconda下建立的虚拟环境(这是我已经创建好的有paddle、pytorch等)

如果你是第一次安装好的话 那么你只会有一个base环境

在这里插入图片描述

这时候我们创建我们自己的pytorch环境(这里我们可以指定python版本号,例如我的是python3.9)

conda create -n pytorch python=3.9

它会让你配置相应环境包 输入y即可(y代表yes)

在这里插入图片描述

这个时候我们的pytorch环境就建立好了,但是我们需要进去配置深度学习相应的包

首先我们得进去pytorch环境中去

conda activate pytorch

在这里插入图片描述

下载相关的环境包是比较慢的,所以我们给环境换源,在pytorch环境下执行如下的命名给环境换清华源。

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

这时我们再打开pytorch的官网(PyTorch),选择我们的版本

在这里插入图片描述

复制好 到我们的pytorch环境下执行

conda install pytorch torchvision torchaudio pytorch-cuda=11.7

在这里插入图片描述

因为我们刚刚进行了换源,不需要把后面的-c pytorch -c nvidia复制过来,那样下载速度会很慢,然后慢慢下载就好啦!

最后可以检查是否安装成功


import torch
print(torch.cuda.is_available())
print(torch.backends.cudnn.is_available())
print(torch.cuda_version)
print(torch.backends.cudnn.version())

这里验证python安装成功

请添加图片描述

在线下载可能会出现一些问题,我这边有适配好的安装包可进行手动安装,具体下载可与我联系。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值