pip 和 Conda 镜像站配置
阅读原文时间:2022年04月05日阅读:1

如果你经常使用 Python,那么你对 pip 和 Conda 一定不陌生,它们作为包管理器,可以非常方便的帮助我们下载需要的 Python 包,但是受限于大多 Python 包的服务器在国外,国内下载速度缓慢,因此需要配置镜像站提升下载速度。

一、pip 镜像站

pip 是一个通用的 Python 包管理器,具有对 Python 包查找、下载、安装、卸载的功能。pip 已内置于 Python3.4 和 2.7 及以上版本中。pip 默认从 PyPI 中下载包,PyPI 全名为 Python Package Index,是 Python 的正式第三方(official third-party)软件包的软件存储库。

PyPI 官网

name

index-url

trusted-host

阿里云

https://mirrors.aliyun.com/pypi/simple/

mirrors.aliyun.com

豆瓣

https://pypi.douban.com/simple/

pypi.douban.com

腾讯云

https://mirrors.cloud.tencent.com/pypi/simple/

mirrors.cloud.tencent.com

华为云

https://repo.huaweicloud.com/repository/pypi/simple/

repo.huaweicloud.com

清华大学

https://pypi.tuna.tsinghua.edu.cn/simple/

pypi.tuna.tsinghua.edu.cn

北京外国语大学

https://mirrors4.bfsu.edu.cn/pypi/web/simple/

mirrors4.bfsu.edu.cn

PyPI

https://pypi.org/

pypi.org

不同地区访问镜像站的速度可能不同,请自行选择合适的镜像站

中科大的 pip 和 Conda 镜像站会重定向到北京外国语大学镜像站,故不列举

查看 pip 安装源信息

pip config list

①.临时使用

每次使用 pip 安装包时指定镜像站

pip install [package-name] -i [index-url] --trusted-host [trusted-host]

例如安装 tensorflow 时使用 阿里云 镜像站

pip install tensorflow -i https://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

②.永久使用

全局使用镜像站,所有包都通过该镜像站下载

pip config set global.index-url [index-url]
pip config set install.trusted-host [trusted-host]

例如全局使用 阿里云 镜像站

pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
pip config set install.trusted-host mirrors.aliyun.com

③.详细配置

Windows

全局使用镜像站后,会在 C:\Users\xxx\AppData\Roaming\pip 中产生一个 pip.ini 文件(C:\Users\xxx\AppData\Roaming\pip\pip.ini)来记录配置信息(如果不存在就自己创建),我们可以修改这个文件进行更详细的配置,下面有一个参考模板:

[global]
timeout = 600
index-url = https://mirrors.aliyun.com/pypi/simple/
extra-index-url = https://pypi.douban.com/simple/
            https://pypi.org/

[install]
trusted-host = mirrors.aliyun.com
            pypi.douban.com
            pypi.org
  • timeout = 600:超时限制为 600 秒

Linux/macOS

全局使用镜像站后,会在用户根目录 ~ 中产生一个隐藏文件夹 .pip,其中的 pip.conf 文件(~/.pip/pip.conf)来记录配置信息(如果不存在就自己创建),具体内容和 Windows 配置一样

二、Conda 镜像站

Conda 是一个开源的软件包和环境管理系统,用于安装多个版本的软件包及其依赖关系,并在它们之间轻松切换。它的包管理与 pip 类似,可以用来管理 Python 的第三方包。

Conda 官网

name

channels

阿里云

https://mirrors.aliyun.com/anaconda/

清华大学

https://mirrors.tuna.tsinghua.edu.cn/anaconda/

北京外国语大学

https://mirrors.bfsu.edu.cn/anaconda/

不同地区访问镜像站的速度可能不同,请自行选择合适的镜像站

查看 Conda 安装源信息

conda info

①.全局使用

conda config --add channels [channels]

例如全局添加 清华大学 镜像站

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
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/r/

②.详细配置

Windows

全局使用镜像站后,会在 C:\Users\xxx 中产生一个隐藏文件 .condarcC:\Users\xxx\.condarc)来记录配置信息(如果不存在就自己创建),我们可以修改这个文件进行更详细的配置,下面有一个参考模板:

channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
  - https://mirrors.bfsu.edu.cn/anaconda/pkgs/main/
  - https://mirrors.bfsu.edu.cn/anaconda/pkgs/free/
  - https://mirrors.bfsu.edu.cn/anaconda/pkgs/r/
  - defaults
ssl_verify: true
show_channel_urls: true
auto_activate_base: false

上述模板展示了 Conda 主要库的镜像源,Conda 附加库的源可以通过 镜像站列表 中的地址访问镜像站后自行添加,例如从 清华大学 镜像站中添加的 pytorch 源地址为 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/

  • ssl_verify: true:开启 SSH 认证
  • show_channel_urls: true:安装包时,显示包的源地址
  • auto_activate_base: false:关闭自动进入 base 环境

Linux/macOS

全局使用镜像站后,会在用户根目录 ~ 中产生一个隐藏文件 .condarc~/.condarc)来记录配置信息(如果不存在就自己创建),具体内容和 Windows 配置一样