提高Python库下载速度的方法 —— conda与pip如何更改为国内镜像

1、pip

对于使用Python官方解释器的用户,可通过pip的指令来更改镜像源,pip的基本指令,只要在cmd命令窗口输入pip便可:

  install                     Install packages.
  download                    Download packages.
  uninstall                   Uninstall packages.
  freeze                      Output installed packages in requirements format.
  list                        List installed packages.
  show                        Show information about installed packages.
  check                       Verify installed packages have compatible dependencies.
  config                      Manage local and global configuration.
  search                      Search PyPI for packages.
  wheel                       Build wheels from your requirements.
  hash                        Compute hashes of package archives.
  completion                  A helper command used for command completion.
  debug                       Show information useful for debugging.
  help                        Show help for commands.

更换国内pypi镜像

国内pypi镜像:
阿里:https://mirrors.aliyun.com/pypi/simple
豆瓣:http://pypi.douban.com/simple
中国科学技术大学:http://pypi.mirrors.ustc.edu.cn/simple/

指定单次安装源:

pip install <包名> -i http://pypi.v2ex.com/simple

指定全局安装源:

在unix和macos,配置文件为:$HOME/.pip/pip.conf
在windows上,配置文件为:%HOME%\pip\pip.ini

[global]
timeout = 6000
index-url = http://pypi.douban.com/simple

下载指定版本安装包:

用 包名 后面加“==” 加 版本号,如

pip install tensorflow==2.0 

2、conda

关于conda,虽然官网的安装速度极慢,但笔者还是不介意更改国内镜像,由于很多国内镜像都停止更新conda的包了,详情见:https://servers.ustclug.org/2019/04/close-anaconda-service/,如果想要更换,可以考虑清华镜像或中科大,但是里面的包都是比较老的版本,但笔者这里也介绍一下更换镜像的方法:

通过 conda config 命令生成配置文件,首先,打开CMD,执行命令:

#清华镜像
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/

#中科大镜像
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/menpo/

#设置搜索时显示通道地址
conda config --set show_channel_urls yes 

此时,目录 C:\Users<你的用户名> 下就会生成配置文件.condarc,找到倒数第二行:

- defaults

将这一行内容删除,保存文件,这时也可以在cmd中用指令

conda info

查看一下channel URLs,如果现实更改的地址说明已经改好,不妨试试,不过比较新的包是没有的。

如果后悔了,想把镜像地址再改为原镜像地址,方法如下:
在cmd中,输入指令:

conda config --remove-key channels
发布了35 篇原创文章 · 获赞 14 · 访问量 6858

猜你喜欢

转载自blog.csdn.net/caoyuan666/article/details/104695229