python包管理工具pip设置国内镜像源
升级最新pip版本
总所周知,在国内下载python依赖包,因为网络原因,非常慢或者无法下载。下面提供两种使用国内镜像的下载方式。
python -m pip install --upgrade pip
会出现类似以下提示表示升级成功。可能用户在安装时会比pip-23.2.1的版本更高。
Installing collected packages: pip
Attempting uninstall: pip
Found existing installation: pip 22.3.1
Uninstalling pip-22.3.1:
Successfully uninstalled pip-22.3.1
Successfully installed pip-23.2.1
国内镜像源
阿里云 http://mirrors.aliyun.com/pypi/simple/
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣 http://pypi.douban.com/simple
Python官方 https://pypi.python.org/simple/
v2ex http://pypi.v2ex.com/simple/
中国科学院 http://pypi.mirrors.opencas.cn/simple/
中国科学技术大学 [http://pypi.mirrors.ustc.edu.cn/simple/
华中理工大学:http://pypi.hustunique.com/
山东理工大学:http://pypi.sdutlinux.org/
阿里云镜像源较为稳定,可以长期使用。如果不设置镜像源,默认采用的时Python官方的镜像源,官方镜像源国内下载较慢或者无法下载。其他镜像源也均可选择。【参考文献】
单次安装使用国内镜像源
单次安装某一依赖包,可使用以下命令,本处以阿里云镜像安装为例。本次安装依赖包为tensorflow,未指定版本时,会安装目前最新的依赖包版本。例如
pip install tensorflow -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
指定版本,可以使用命令如下:
pip install tensorflow==2.13.0 -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
永久设置国内镜像源
以设置阿里云镜像为例:
pip config set global.index-url http://mirrors.aliyun.com/pypi/simple/
pip config set global.trusted-host mirrors.aliyun.com
综上,使用国内镜像源可以大幅度加速下载。同时在设置镜像源时,要同时设置index-url和trusted-host,否则还是显示报错。