【Python】anaconda、pip配置国内镜像

1. Anconda配置国内镜像

顺序执行就行,简单高效方便省事,每一行都做了详细的注解,大家再也不用闷头执行也不知道在干嘛了。

# 移除当前配置的所有镜像(会保留default)
conda config --remove-key channels
# 添加清华镜像,建议下面全部添加,虽然有些表面上可能用不到,但实际可能包含比较冷门的包,还可以加速下载
# 已为conda构建但尚未成为官方Anaconda分发一部分的包
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/osx-64
# 自由软件镜像
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/osx-64
# 主要软件镜像
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/osx-64
# fastai镜像
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/fastai/osx-64
# pytorch镜像
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/osx-64
# bioconda镜像
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/osx-64

conda config --set show_channel_urls yes
# 查看conda的配置,确认channels
conda config --show 
# 仅查看所有镜像
conda config --show-sources 

2. pip配置国内镜像

创建环境

# 创建环境
conda create -n dp_env python==3.8
# 激活环境,成功激活后命令行会有(dp_env)前缀
conda activate dp_env
# 退出环境
conda deactivate

每次创建python的新环境之后都要执行如下命令,修改pip镜像地址为国内镜像

# 设置pip国内镜像
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
# 更新pip
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U

如果需要指明某个镜像安装装包,加上参数-i 镜像地址即可:

pip install numpy -i http://pypi.douban.com/simple

可选地址:

清华大学https://pypi.tuna.tsinghua.edu.cn/simple/
阿里云http://mirrors.aliyun.com/pypi/simple/
中国科技大学https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣http://pypi.douban.com/simple/
中国科学技术大学http://pypi.mirrors.ustc.edu.cn/simple/