9、Anaconda 快速入门


一、Anaconda 是什么?

Anaconda 是一个包含数据科学常用包的发行版本。它基于 conda(一个包和环境管理器) 衍生而来。应用程序 conda 是包和环境管理器:

  • 使用 conda 创建虚拟环境,可以方便的分隔使用不同 Python 版本和/或不同包的项目
  • 可使用conda环境中安装、卸载和更新

二、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_name
  • conda info:Display information about current conda install
  • conda help:Displays a list of available conda commands and their help strings
  • conda search:Search for packages(可以进行模糊匹配) and display their information
  • conda create:Create a new conda environment from a list of specified packages
  • conda command --help(-h):For full documentation of any command, type the command followed by --help(-h)
  • conda -V:Show the conda version number and exit
  • conda 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 镜像

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值