Anaconda国内镜像汇总(conda & pip)

conda

临时切换通道举例:

conda install pytorch torchvision -c https://mirrors6.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/

长期切换通道,推荐生成.condarc文件后手动编辑文件内容,而不是通过命令行一个一个添加channel

.condarc文件默认不生成,运行一下命令就可以在用户目录下生成:conda config –set show_channel_urls yes

在.condarc文件中复制以下内容(找不到该文件的可以conda info看看"user config file"的路径)

# This is a sample .condarc file.
# It adds mirror-channel(Tsinghua University) of anaconda and enables
# the show_channel_urls option.

# channel locations. These override conda defaults, i.e., conda will
# search *only* the channels listed here, in the order given.
# Use "defaults" to automatically include all default channels.
# Non-url channels will be interpreted as Anaconda.org usernames
# (this can be changed by modifying the channel_alias key; see below).
# The default is just 'defaults'.
channels:
  - defaults
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
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

# Show channel URLs when displaying what is going to be downloaded
# and in 'conda list'. The default is False.
show_channel_urls: true

# For more information about this file see:
# https://conda.io/docs/user-guide/configuration/use-condarc.html

然后conda clean -i 清除索引缓存

以上能用的情况下,不想麻烦事儿的可以不看下边内容了

以上用的是清华源,如果想切换北外、阿里云、中科大、上交镜像的话,可以将上面的"https://mirrors.tuna.tsinghua.edu.cn/anaconda"分别替换为:

https://mirrors.bfsu.edu.cn/anaconda (北外,清华的姊妹站)
https://mirrors.aliyun.com/anaconda (阿里云,比较新,之前只有pypi镜像)
https://mirrors.ustc.edu.cn/anaconda (中科大:由于合规性Anaconda源目前已经无限期停止服务)
https://anaconda.mirrors.sjtug.sjtu.edu.cn (上交)

此外,主域名也可手动替换:

清华镜像域名选择
https://mirrors.tuna.tsinghua.edu.cn 自动选择
https://mirrors6.tuna.tsinghua.edu.cn 只解析 IPv6
https://mirrors4.tuna.tsinghua.edu.cn 只解析 IPv4
北外镜像域名选择
https://mirrors.bfsu.edu.cn 自动选择
https://mirrors6.bfsu.edu.cn 只解析 IPv6
https://mirrors4.bfsu.edu.cn 只解析 IPv4
中科大域名选择
mirrors.ustc.edu.cn 自动解析
ipv4.mirrors.ustc.edu.cn IPv4 线路
ipv6.mirrors.ustc.edu.cn IPv6 线路
cernet.mirrors.ustc.edu.cn 教育网线路
chinanet.mirrors.ustc.edu.cn 电信线路
unicom.mirrors.ustc.edu.cn 联通线路
cmcc.mirrors.ustc.edu.cn 移动线路
rsync.mirrors.ustc.edu.cn Rsync 线路

为什么推荐手动编辑配置文件呢?

因为conda config –add channels new_channel这个命令是往"channels"里面添加新通道,比如添加一个https://mirrors6.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/,但这样添加的话使用命令conda install pytorch -c pytorch会有问题,conda识别出-c给的参数不是URL会自动把通道补全变成https://conda.anaconda.org/pytorch/,依然访问源站,并没有用上tuna的镜像,然后有的博客就教不要加"-c pytorch",意思是从添加的通道里面去找包,可是少数情况下其他包并不通用,都应该像custom_channels那种写法才对,推荐生成配置文件手动往里面编辑吧

如果非要手动添加通道,命令如下:

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r/
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 --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/
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/simpleitk/

要注意,conda config --add channels等价于conda config --prepend channels,
是在第一个channel前添加,即后添加的优先级高,优先在其中搜索。

换回默认源的命令:
conda config --remove-key channels

额外一点知识:

同时传入两个通道时优先级逐渐降低
e.g. conda install scipy -c conda-forge -c bioconda
使用–override-channels仅搜索指定的频道,忽略".condarc"中配置的任何通道,也忽略了默认通道
e.g. conda install scipy -c bioconda –override-channels
想换回默认源删除配置文件就行

pip

临时使用:

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package

不行的话加参数--trusted-host pypi.tuna.tsinghua.edu.cn

长期使用:

​pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

长期使用也可以创建用户目录下的配置文件(~/.pip/pip.conf or C:\Users\WQP\pip\pip.ini),填入下面内容:

[global] 
index-url = https://pypi.tuna.tsinghua.edu.cn/simple #清华镜像,可以换成其他的镜像
trusted-host = pypi.tuna.tsinghua.edu.cn #添加清华源为可信主机,要不然可能报错
disable-pip-version-check = true #取消pip版本检查,排除每次都报最新的
pip timeout = 120

国内pip镜像源:

清华:https://pypi.tuna.tsinghua.edu.cn/simple
北外:https://mirrors.bfsu.edu.cn/pypi/web/simple
阿里云:https://mirrors.aliyun.com/pypi/simple/
中国科技大学:https://pypi.mirrors.ustc.edu.cn/simple/
华中科技大学:http://pypi.hustunique.com/
山东理工大学:http://pypi.sdutlinux.org/
豆瓣:http://pypi.douban.com/simple/

新版ubuntu要求使用https源,要注意。Noarch is a contraction of "no architecture".

偶然发现阿里云开发者社区的镜像网站:https://developer.aliyun.com/mirror/

以上,2021年11月12日

来源:yiteeee

物联沃分享整理
物联沃-IOTWORD物联网 » Anaconda国内镜像汇总(conda & pip)

发表评论