conda-包管理工具

conda-包管理工具

conda作为经典的包管理工具, 在深度学习的过程中经常会用到. 这里从安装, 配置, 环境配置以及工作流的角度来简单介绍一下conda的使用方法.

conda的安装

conda一般分为Anaconda和Miniconda两种:

  • Anaconda 是一个用于科学计算的conda发行版,支持 Linux, Mac, Windows, 包含了众多流行的科学计算、数据分析的 Python 包。

  • Miniconda 是一个 Anaconda 的轻量级替代,默认只包含了 python 和 conda,可以通过 pip 和 conda 来安装所需要的包。

Anaconda略显冗余, 一般建议直接使用Miniconda.

先通过wget命令从清华源上下载对应的安装包. 然后执行.sh文件进行安装即可.

conda的配置

conda的配置文件为.condarc 具体配置可以直接在.condarc文件中修改, 也可以执行conda config命令进行修改.

Windows无法直接创建.condarc文件, 可以使用conda config --set show_channel_urls yes命令来生成该文件.

正常需要自己改的只有channels选项. 其他的用预设的选项就好了.

# 使用清华源的conda配置
## 正常情况下采用conda-forge通道.
## conda config --add channels conda-forge (添加通道)
## conda config --get channels (查看通道)
## conda config --set channel_priority strict (严格保证channel优先级)
channels:
  - conda-forge
  - defaults
show_channel_urls: true
## 设置默认通道的url地址.(改成国内镜像清华源)
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
## 设置自定义通道的url地址. (同样使用清华源)
custom_channels:
  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  conda-forge: https://anaconda.org/conda-forge
  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
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

conda env的配置

使用environment.yml描述环境配置.

yml全称(why a makeup language)是与json类似的基于key-value pairs的数据文件格式. 基于层级缩进来描述数组, 字典及其嵌套的数据格式. 常用于作为配置文件使用.

name: fsdl-text-recognizer-2021 # 环境名
channels:												# 指明该环境所用的通道 (一般常用conda-forge)
  - defaults
dependencies:										# 指明环境所需依赖 (以数组格式进行描述 - <name>=<version>)
  - python=3.6  
  - cudatoolkit=10.1
  - cudnn=7.6
  - pip
  - pip:												# 使用pip管理的依赖 (同样以数组格式进行描述)
    - pip-tools

conda的工作流

conda config

conda config命令用于管理yml格式的 .condarc文件, 从而完成对conda的配置.

# conda config 命令的用法
## 通过对.condarc这个yml格式的配置文件进行crud来进行配置
conda config -h 						 # 查看help文档
conda config --add key value # 添加配置
conda config --get key 			 # 查询配置的value
conda config --set key value # 修改配置的value

conda

这里直接给出conda的help文档内容. 现用现查

常用命令:

  • conda activate <env_name> 激活环境
  • conda deactivate <env_name> 关闭环境
  • conda clean 清除缓存
conda is a tool for managing and deploying applications, environments and packages.

Options:

positional arguments:
  command
    clean        Remove unused packages and caches.
    compare      Compare packages between conda environments.
    config       Modify configuration values in .condarc. This is modeled
                 after the git config command. Writes to the user .condarc
                 file (/Users/yuyiling/.condarc) by default.
    create       Create a new conda environment from a list of specified
                 packages.
    help         Displays a list of available conda commands and their help
                 strings.
    info         Display information about current conda install.
    init         Initialize conda for shell interaction. [Experimental]
    install      Installs a list of packages into a specified conda
                 environment.
    list         List linked packages in a conda environment.
    package      Low-level conda package utility. (EXPERIMENTAL)
    remove       Remove a list of packages from a specified conda environment.
    uninstall    Alias for conda remove.
    run          Run an executable in a conda environment.
    search       Search for packages and display associated information. The
                 input is a MatchSpec, a query language for conda packages.
                 See examples below.
    update       Updates conda packages to the latest compatible version.
    upgrade      Alias for conda update.

optional arguments:
  -h, --help     Show this help message and exit.
  -V, --version  Show the conda version number and exit.

conda commands available from other packages:
  build
  content-trust
  convert
  debug
  develop
  env
  index
  inspect
  metapackage
  render
  repo
  server
  skeleton
  token
  verify

conda env

conda的环境管理命令, 这里直接给出conda env 的help文档, 现查现用

常用命令:

  • conda env export > environment.yml 导出当前环境配置
  • conda env create -f environment.yml 从配置文件中创建环境
  • conda env remove -n <env_name> 删除环境
  • conda env update -f environment.yml 根据配置文件更新环境
  • conda env config 更改环境配置
usage: conda-env [-h] {create,export,list,remove,update,config} ...

positional arguments:
  {create,export,list,remove,update,config}
    create              Create an environment based on an environment file
    export              Export a given environment
    list                List the Conda environments
    remove              Remove an environment
    update              Update the current environment based on environment file
    config              Configure a conda environment

optional arguments:
  -h, --help            Show this help message and exit.

conda commands available from other packages:
  build
  content-trust
  convert
  debug
  develop
  env
  index
  inspect
  metapackage
  render
  repo
  server
  skeleton
  token
  verify

source

conda官方文档: https://docs.conda.io/projects/conda/en/latest/index.html

清华源镜像站: https://mirrors.tuna.tsinghua.edu.cn


版权声明:本文为weixin_45240960原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。