pip 和 Conda 镜像站配置

2022年5月26日10:07:46

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

本文首发于正切橙的博客,更多详细信息请点击这里,转载请注明出处。


一、pip 镜像站

1.pip 是什么

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

pip 和 Conda 镜像站配置

PyPI 官网

2.镜像站列表

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 镜像站会重定向到北京外国语大学镜像站,故不列举

3.如何配置

查看 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 镜像站

1.Conda 是什么

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

pip 和 Conda 镜像站配置

Conda 官网

2.镜像站列表

name channels
阿里云 https://mirrors.aliyun.com/anaconda/
清华大学 https://mirrors.tuna.tsinghua.edu.cn/anaconda/
北京外国语大学 https://mirrors.bfsu.edu.cn/anaconda/
Anaconda(默认仓库) https://repo.anaconda.com/pkgs/ (defaults)

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

3.如何配置

查看 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/

切换镜像站后清除索引缓存,保证用的是镜像站提供的索引

conda clean -i

②.详细配置

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 环境

同样使用conda clean -i 清除一下缓存

Linux/macOS

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

  • 作者:正切橙
  • 原文链接:https://www.cnblogs.com/oranhext/p/py-mirrors.html
    更新时间:2022年5月26日10:07:46 ,共 3434 字。