分享

解密conda channels

 生信修炼手册 2020-12-09
channels是conda下载包的镜像网站,通过如下命令可以查看已有的channels
conda config --show channels
channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
  - 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/menpo/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
  - defaults
以清华的镜像为例

>https://mirrors.tuna./anaconda/pkgs/free/

每个url对应的是不同操作系统平台的文件夹

在操作系统对应的目录下,是具体的安装包,后缀为tar.bz2

>https://mirrors.tuna./anaconda/pkgs/free/linux-64/

在这些安装包中,有一个比较特殊repodata.json, 存储了该目录下包含的所有包的名称,依赖,md5等信息

部分内容如下

{
  "info": {
 "arch": "x86_64",
 "platform": "linux",
 "subdir": "linux-64"
  },
  "packages": {
 "_libgcc_mutex-0.1-free.tar.bz2": {
   "build": "free",
   "build_number": 0,
   "date": "2019-07-01",
   "depends": [],
   "md5": "f7d7639c8485ae4701aeb6add534a774",
   "name": "_libgcc_mutex",
   "size": 3129,
   "timestamp": 1562011672620,
   "track_features": "free_channel_libgcc",
   "version": "0.1"
 },
 "_license-1.1-py27_0.tar.bz2": {
   "build": "py27_0",
   "build_number": 0,
   "date": "2013-03-01",
   "depends": [
  "python 2.7*"
   ],

在安装包的时候,conda会依次遍历所有的channnels,通过repodata来查找该channel是否包含需要下载的packages,  一个基本的安装过程如下

conda create -n myenv numpy
Collecting package metadata (current_repodata.json): done
Solving environment: done


## Package Plan ##


  environment location: /media/gsadmin/vd1/heguangliang/bcbio3/anaconda/envs/myenv


  added / updated specs:
 - numpy




The following packages will be downloaded:


 package | build
 ---------------------------|-----------------
 blas-1.0 | mkl 6 KB https://mirrors.tuna./anaconda/pkgs/free
 numpy-1.13.1   | py36_0 7.2 MB https://mirrors.tuna./anaconda/pkgs/free
 ------------------------------------------------------------
 Total: 7.2 MB


The following NEW packages will be INSTALLED:


  blas anaconda/pkgs/free/linux-64::blas-1.0-mkl
  certifi anaconda/pkgs/free/linux-64::certifi-2016.2.28-py36_0
  mkl anaconda/pkgs/free/linux-64::mkl-2017.0.3-0
  numpy anaconda/pkgs/free/linux-64::numpy-1.13.1-py36_0
  openssl anaconda/pkgs/free/linux-64::openssl-1.0.2l-0
  pip anaconda/pkgs/free/linux-64::pip-9.0.1-py36_1
  python anaconda/pkgs/free/linux-64::python-3.6.2-0
  readline anaconda/pkgs/free/linux-64::readline-6.2-2
  setuptools anaconda/pkgs/free/linux-64::setuptools-36.4.0-py36_1
  sqlite anaconda/pkgs/free/linux-64::sqlite-3.13.0-0
  tk anaconda/pkgs/free/linux-64::tk-8.5.18-0
  wheel anaconda/pkgs/free/linux-64::wheel-0.29.0-py36_0
  xz anaconda/pkgs/free/linux-64::xz-5.2.3-0
  zlib anaconda/pkgs/free/linux-64::zlib-1.2.11-0




Proceed ([y]/n)?

Downloading and Extracting Packages
numpy-1.13.1   | 7.2 MB | ################################################################################################################################# | 100%
blas-1.0 | 6 KB | ###
############################################################################################################################## | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
# $ conda activate myenv
#
# To deactivate an active environment, use
#
# $ conda deactivate

对于某些不能用的镜像,会在下载repodata.json时失效,出现如下所示的报错

conda create -n myenv ggtree
Collecting package metadata (current_repodata.json): failed

CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://mirrors.tuna./anaconda/cloud/conda-forge/linux-64/current_repodata.json>
Elapsed: -

An HTTP error occurred when trying to retrieve this URL.
HTTP errors are often intermittent, and a simple retry will get you on your way.
ConnectionError(ReadTimeoutError("HTTPSConnectionPool(host='mirrors.tuna.', port=443): Read timed out.",),)

此时我们就需要确认下该镜像是失效了,还是因为网络原因,如果失效的话,就考虑删除这个镜像。

当网络波动时,会出现下载失败的情况,报错如下

CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64/mkl-2020.2-256.conda>
Elapsed: -

An HTTP error occurred when trying to retrieve this URL.
HTTP errors are often intermittent, and a simple retry will get you on your way.

此时镜像本身是没问题的,就是下载速度慢,导致下载失败,只需要多试几次即可。

对于channels而言,我们需要学会新增和删除,对应的操作如下

1. 删除

conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/

2. 增加

conda config --add channels https://mirrors.tuna./anaconda/pkgs/free/

config子命令的本质是在操作.condarc这个配置文件,所以我们直接在这个配置文件里进行修改也是可以的,condarc的内容示例如下

channels:
  - https://mirrors.tuna./anaconda/pkgs/free/
  - https://mirrors.tuna./anaconda/pkgs/main/
  - https://mirrors.tuna./anaconda/cloud/bioconda/
  - https://mirrors.tuna./anaconda/cloud/menpo/
  - https://mirrors.tuna./anaconda/cloud/msys2/
  - defaults
ssl_verify: false
show_channel_urls: true

修改镜像的使用场景有以下两种,第一种是使用国内镜像,提高下载速度,第二种是为了下载特定镜像才包含的packages。镜像的修改非常简单,关键是要理解镜像的本质。

·end·

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多