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日

当然可以。`conda` 和 `pip` 都是中国开发者常用的 Python 包管理工具,为了加快下载速度、提高安装效率,通常会设置国内镜像源。 ### pip 设置国内镜像 对于 `pip`, 我们可以通过指定命令行选项或修改配置文件的方式来使用国内镜像: #### 使用临时方法,在每次安装包的时候加上 `-i 参数` ```bash pip install <package_name> -i https://pypi.tuna.tsinghua.edu.cn/simple ``` #### 修改全局配置文件 (推荐) 你可以通过编辑 ~/.pip/pip.conf 文件(Windows 系统下是 %HOMEPATH%\pip\pip.ini) 来永久更改默认索引 URL: ```ini [global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple ``` 也可以直接运行以下指令来创建该配置文件并添加内容: ```bash python -m pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple ``` ### Conda 设置国内镜像 对于 `Conda`, 它也支持类似的方式来进行加速下载: #### 添加清华等国内镜像到当前环境的 channels 列表里 ```bash conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/ conda config --set show_channel_urls yes # 将默认 channel 更改为 tsinghua 源 conda config --prepend channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main ``` #### 直接在命令行中使用特定 mirror 地址 例如从 Tsinghua 的 Anaconda 镜像安装 numpy: ```bash conda install --channel=https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/mc/numpy ``` 以上就是关于如何为 `conda` 和 `pip` 工具配置中国区镜像的方法了。如果你还有其他疑问或者其他想要了解的内容,请告诉我!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值