一、Anaconda 是什么?
Anaconda
是一个包含数据科学常用包的发行版本。它基于conda(一个包和环境管理器)
衍生而来。应用程序conda
是包和环境管理器:
- 使用
conda
创建虚拟环境,可以方便的分隔使用不同 Python 版本和/或不同包的项目- 可使用
conda
在环境中安装、卸载和更新包。
二、Anaconda 的安装、升级及卸载
- Anaconda 的安装
- Anaconda 可用于 Windows、Mac OS X 和 Linux。可以在 https://www.continuum.io/downloads 上找到安装程序
- linux 下安装:
bash Anaconda2-4.3.1-Linux-x86_64.sh
- 按
q
键跳过license
文件,然后输入yes
确认 - 把
anaconda
的bianry
路径加入到.bashrc
- windows 下安装:按照提示一步步安装即可
- linux 下安装:
- 有时候从官网上下载此软件特别慢,所以在此提供百度云下载链接,希望对大家有帮助
- Anaconda 可用于 Windows、Mac OS X 和 Linux。可以在 https://www.continuum.io/downloads 上找到安装程序
- Anaconda 的升级
conda update conda
conda update anaconda
- Anaconda 的卸载
rm -rf anaconda
# 建议清理下 .bashrc 中 anaconda 的路径
三、管理包
conda list
:列出所有已安装的包conda install package_name
:安装软件包,同时它会自动安装此软件包的依赖项conda install numpy pandas
:同时安装多个包conda install python=2.7
:安装指定版本的包conda -c conda-forge install package
:anaconda 源中找不到相应的包时,可尝试使用conda-forge
查找
conda remove package_name
:卸载包conda update/upgrade --all
:更新环境中的所有已安装的包conda update package_name
:只更新特定的包 package_nameconda info
:Display information about current conda installconda help
:Displays a list of available conda commands and their help stringsconda search
:Search for packages(可以进行模糊匹配) and display their informationconda create
:Create a new conda environment from a list of specified packagesconda command --help(-h)
:For full documentation of any command, type the command followed by --help(-h)conda -V
:Show the conda version number and exitconda clean
- 通过
conda clean -p
来删除一些没用的包,这个命令会检查哪些包没有在包缓存中被硬依赖到其他地方,并删除它们 - 通过
conda clean -t
可以将 conda 保存下来的 tar 包
- 通过
四、管理环境
- 创建虚拟环境:
conda create -n env_name list of packages
,在这里,-n env_name
设置环境的名称(-n 是指名称),而 list of packages 是要安装在环境中的包的列表- 可以创建具有特定 Python 版本的环境,
conda create -n py2 python=2
:安装 Python2 的最新版本conda create -n tf_15 python=2 anaconda opencv
:安装 Python2 最新版本的同时安装 anaconda 和 opencv 相关的包
- anaconda2 下安装 anaconda3 的虚拟环境:
bash Anaconda2*.sh
bash Anaconda3*.sh -b -p anaconda2/envs/py3
conda activate py3 即可使用 python3 环境
- 进入环境:linux 下用
conda activate env_name
, windows 下用activate env_name
- 离开环境:linux 下用
conda deactivate
, windows 下用deactivate
- 列出环境:
conda env list
- 删除环境:
conda env remove -n env_name
,默认的环境(即当你不在环境中时使用的环境)名为 root。 - 导出环境:
conda env export > environment.yaml
,将包保存为 YAML。第一部分conda env export
输出环境中的所有包的名称(包括 Python 版本) - 加载环境:
conda env create -f environment.yaml
# environment.yaml 示例 name: gluon dependencies: - python=3.6 - pip: - mxnet==1.4.0 - d2lzh==0.8.11 - jupyter==1.0.0 - matplotlib==2.2.2 - pandas==0.23.4
五、更改 Anaconda 下载源
- 有时使用 conda 下载软件包
速度比较慢
,这里我们更改到国内的源
来解决此问题
# 一、anaconda 源:windows&linux 都适用
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 --set show_channel_urls yes
# 二、anaconda 维护的第三方源
# 1、pytorch
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/peterjc123/ # for legacy win-64(linux 不用添加)
# 2、menpo
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/
# 3、Conda Forge
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
# 4、bioconda
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
# 5、msys2
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
# 三、.condarc 中出现如下内容,channels 是查找源的顺序
channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
- defaults
show_channel_urls: true
# 四、添加完记得执行 sudo apt-get update 更新软件列表
六、多个用户共用一个 anaconda
# 1、install Anconda (/opt/anaconda2)
# 2、create a new group
sudo groupadd ai_edge
# 3、Change the group ownership to "ai_edge" on the entire directory where Anaconda is installed
sudo chgrp -R ai_edge /opt/anaconda2
# 4、Set read and write permission for the owner, root and the mygroup only
sudo chmod 770 -R /opt/anaconda2
# 5、Add user(eg: manzp) to a group(eg: ai_edge)
sudo adduser manzp ai_edge
# 6、add the anaconda path to your .bashrc file
export PATH="/opt/anaconda2/bin:$PATH"
# 7、source .bashrc to make it effective
source .bashrc
七、参考资料
1、Udacity 深度学习纳米课程
2、Anaconda 清华镜像使用帮助
3、conda doc
4、.condarc 说明
5、Multi User Anaconda Installation on Linux
6、阿里云 Anaconda 镜像