前言
在学习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安装成功
在线下载可能会出现一些问题,我这边有适配好的安装包可进行手动安装,具体下载可与我联系。