Anaconda和Miniconda简介
Anaconda 是专门为了方便使用 Python 进行数据科学研究而建立的一组软件包,涵盖了数据科学领域常见的 Python 库,并且自带了专门用来解决软件环境依赖问题的 conda 包管理系统。主要是提供了包管理与环境管理的功能,可以很方便地解决多版本python并存、切换以及各种第三方包安装问题。Anaconda利用工具/命令conda来进行package和environment的管理,并且已经包含了Python和相关的配套工具。
conda可以理解为一个工具,也是一个可执行命令,其核心功能是包管理与环境管理。包管理与pip的使用类似,环境管理则允许用户方便地安装不同版本的python并可以快速切换。
Anaconda则是一个打包的集合,里面预装好了conda、某个版本的python、众多packages、科学计算工具等等,所以也称为Python的一种发行版。其实还有Miniconda,它只包含最基本的内容——python与conda,以及相关的必须依赖项,对于空间要求严格的用户,Miniconda是一种选择。
conda将几乎所有的工具、第三方包都当做package对待,甚至包括python和conda自身!因此,conda打破了包管理与环境管理的约束,能非常方便地安装各种版本python、各种package并方便地切换。
Linux环境安装配置
下载Anaconda
Anaconda下载网址: https://www.anaconda.com/products/distribution
最新版Anaconda下载链接:
https://repo.anaconda.com/archive/Anaconda3-2022.10-Linux-x86_64.sh
Linux下安装命令:
bash Anaconda3-2022.10-Linux-x86_64.sh #安装Anaconda
Anaconda换源
修改condarc文件
vim ~/.condarc
内容如下( 来源网址:https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/ ):
channels:
- defaults
show_channel_urls: true
default_channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
清除索引缓存,保证用的是镜像站提供的索引:
conda clean -i
安装环境
创建环境
conda create –n d2l python=3.9
创建完成后激活环境
conda activate d2l
使用pip 安装库文件
pip install numpy pandas matplotlib torch torchvision torchaudio jupyterlab -i https://mirror.sjtu.edu.cn/pypi/web/simple
git安装
# ubuntu 环境
apt install git
# CentOS环境
yum install git
下载课程代码
git clone https://openi.pcl.ac.cn/Datawhale/d2l/
此处的链接也可以替换成fork后自己的代码仓地址
环境启动
先设置jupyter lab 密码
jupyter lab password
启动jupyterlab
# 如果使用root账户进行实验的话需要加上—allow-root
Jupyter lab –-ip 0.0.0.0 –-allow-root
Windows环境安装配置
下载Miniconda
Minicodna 下载地址: https://docs.conda.io/en/latest/miniconda.html
最新版 Miniconda 下载链接:
https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe
下载后直接安装即可。
更换镜像源
Conda更换镜像源
上海交通大学 Linux 用户组 软件镜像服务 Conda 镜像源 :
https://mirrors.sjtug.sjtu.edu.cn/docs/anaconda
复制文本后,在 Anaconda Powershell Prompt 中输入:
#注意有个小点.在"condarc"的前面
> notepad .condarc
粘贴刚刚复制的文本,保存文件后关闭
default_channels:
- https://mirror.sjtu.edu.cn/anaconda/pkgs/r
- https://mirror.sjtu.edu.cn/anaconda/pkgs/main
custom_channels:
conda-forge: https://mirror.sjtu.edu.cn/anaconda/cloud/
pytorch: https://mirror.sjtu.edu.cn/anaconda/cloud/
channels:
- defaults
最后在 Anaconda Powershell Prompt 中输入:
#清除源缓存,以应用镜像源
> conda clean -i
PyPi更换镜像源
上海交通大学 Linux 用户组 软件镜像服务 PyPI 镜像源 :
https://mirrors.sjtug.sjtu.edu.cn/docs/pypi-packages
复制文本后,在 Anaconda Powershell Prompt 中粘贴运行即可:
#设置PyPI镜像源
pip config set global.index-url https://mirror.sjtu.edu.cn/pypi/web/simple
GPU 版本 Pytorch 镜像源可见:https://mirrors.sjtug.sjtu.edu.cn/docs/pytorch-wheels
Git安装与使用
下载地址:https://git-scm.com/download/
加速地址:https://ghproxy.com/
Git学习地址: https://missing-semester-cn.github.io/
下载课程代码
在指定目录打开终端,或者使用 cd 命令改变终端路径,例如:C:\Coding\d2l
cd C:\Coding\d2l
在当前终端中输入:
# d2l 课程资源
git clone https://github.com/d2l-ai/d2l-zh.git
# d2l 幻灯片资源
git clone https://github.com/d2l-ai/d2l-pytorch-slides.git
课程环境搭建
创建与激活Conda
创建 Conda 环境:
# conda环境创建
> conda create -n d2l python=3.9
其中 -n 代表创建的环境名称,这里是 d2l,并指定 Python 版本为 3.9 (经过 d2l 测试)
激活刚刚创建的 Conda 环境:
#激活d2l环境,不同环境包版本不同!
> conda activate d2l
如果需要删除某个 Conda 环境:
# 退出该环境
> conda deactivate
#删除整个环境
> conda remove -n d2l --all
第三方库安装
Pip 安装课程所需第三方库:
#必装库
> pip install d2l torch
#所有库
> pip install d2l torch torchvision rise
在课程内容路径输入:
#会自动跳转到浏览器
> jupyter-notebook
结束学习时使用:
#关闭jupyter-notebook服务
> Ctrl+C