Python/pip 和 conda 修改源

Python/pip 和 conda 修改源

pip 修改源

pip 管理工具安装库文件时,默认从国外的源下载。

导致安装库的时候,下载速度非常慢,切换成国内的镜像源,可以大大提升下载速度。

比较常用的国内镜像有:

阿里云 https://mirrors.aliyun.com/pypi/simple/

豆瓣 http://pypi.douban.com/simple/

清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/

中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/

华中科技大学 http://pypi.hustunique.com/

一种方法是直接修改 pip 的配置文件,但是不同的系统(Windows、Linux、Mac)的配置文件的路径不一样,而且随着 pip 版本的不同,文件位置也会变动。

我之前根据网上搜索的教程,大部分都是直接修改配置文件的。但是没有生效,后来发现是 pip 的配置文件路径变了。

因此不推荐这种方法。

最佳实践是使用命令来修改源

比如要把源修改为阿里云。

命令如下:

1
2
3
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/

pip config set install.trusted-host https://mirrors.aliyun.com

注意,如果使用 pip config 报错,意味着你的 pip 版本太低了,需要先通过如下命令升级 pip

1
python -m pip install --upgrade pip

conda 修改源

命令修改

添加清华源的命令如下:

1
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/

添加完成后,我们使用 conda config --show channels 命令查看所有的源,显示如下:

1
2
3
channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
- defaults

可以看到,现在我们有两个源,一个清华镜像,一个 defauls 默认下载源。

但我们在安装库的时候,依然会使用 conda install opencv,还是会使用 defaults 源,不会使用清华源。

还是会报错。

1
2
3
4
5
6
7
8
9
10
11
12
Collecting package metadata (current_repodata.json): ...working... failed

CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://repo.anaconda.com/pkgs/main/win-64/current_repodata.json>
Elapsed: -

An HTTP error occurred when trying to retrieve this URL.
HTTP errors are often intermittent, and a simple retry will get you on your way.

If your current network has https://www.anaconda.com blocked, please file
a support request with your network engineering team.

SSLError(MaxRetryError('HTTPSConnectionPool(host=\'repo.anaconda.com\', port=443): Max retries exceeded with url: /pkgs/main/win-64/current_repodata.json (Caused by SSLError(SSLError("bad handshake: SysCallError(10054, \'WSAECONNRESET\')",),))',),)

我们需要修改配置文件,删除 defaults 源。

conda 的配置文件路径是在用户目录中。

在 Mac 和 Linux 中,conda 的配置文件路径是 ~/.condarc

在 Windows 中,conda 的配置文件路径是在 C 盘的用户目录下,如 C:\Users\lenovo/.condarc

Windows 用户无法直接创建名为 .condarc 的文件,可先执行 conda config --set show_channel_urls yes 生成该文件之后再修改。

1
conda config --set show_channel_urls yes

打开 .condarc 文件,我们可以看到如下内容:

1
2
3
4
5
ssl_verify: true
channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
- defaults
show_channel_urls: true

- defaults 删除,再次安装库,就会从清华源下载。

修改配置文件(推荐)

关于 conda 修改源,网上搜索到的教程大部分类似如下:

1
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/

然而上面的添加方式是不全面的,实际需要添加比较多的 channels,因此更加推荐的做法是修改配置文件

首先到 Anaconda 清华源官方网站 查看源的配置文件,如下图所示:


我们需要把清华源网站的配置,复制到本地的 conda 配置文件。

conda 的配置文件路径是在用户目录中。

在 Mac 和 Linux 中,conda 的配置文件路径是 ~/.condarc

在 Windows 中,conda 的配置文件路径是在 C 盘的用户目录下,如 C:\Users\lenovo\.condarc

Windows 用户无法直接创建名为 .condarc 的文件,可先执行 conda config --set show_channel_urls yes 生成该文件之后再修改。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
channels:
- defaults
show_channel_urls: true
channel_alias: https://mirrors.tuna.tsinghua.edu.cn/anaconda
default_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/pkgs/pro
- 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

评论