python默认的源为国外的pypi源(https://pypi.org/simple),在国内使用速度较慢,为了便于下载第三方包,可以使用国内镜像源,命令如下:
临时切换源
pip install <安装包> -i <镜像源>
示例:
临时使用阿里源安装requests包
pip install requests -i https://mirrors.aliyun.com/pypi/simple/
永久性切换源
方法一:命令行方式
pip config set global.index-url <镜像源>
永久性切换源后,直接执行以下语句即可:
pip install <安装包>
方法二:修改配置文件方式
LINUX:
新增/修改pip配置文件~/.pip/pip.conf:
[global]
index-url=http://pypi.douban.com/simple
[install]
trusted-host=pypi.douban.com
Windows:
新增/修改pip配置文件%HOMEPATH%\pip\pip.ini(一般为C:\Users\<当前用户名>\AppData\Roaming\pip\pip.ini),内容与linux内容一致。
恢复为默认源
pip config unset global.index-url
常内国内源
清华源:
https://pypi.tuna.tsinghua.edu.cn/simple
阿里源:
https://mirrors.aliyun.com/pypi/simple/
腾讯源:
http://mirrors.cloud.tencent.com/pypi/simple
中国科技大学:
https://pypi.mirrors.ustc.edu.cn/simple/
华中科技大学:
http://pypi.hustunique.com/simple/
上海交通大学:
https://mirror.sjtu.edu.cn/pypi/web/simple/
豆瓣:
http://pypi.douban.com/simple/
常见问题及处理
安装包的时候出现:
Collecting flask
The repository located at mirrors.aliyun.com is not a trusted or secure host and is being ignored. If this repository is available via HTTPS it is recommended to use HTTPS instead, otherwise you may silence this warning and allow it anyways with ‘–trusted-host mirrors.aliyun.com’.
Could not find a version that satisfies the requirement flask (from versions: )
No matching distribution found for flask
这个问题是因为阿里镜像源并非信任的源,加入信任即可:
1.临时信任源
pip install flask --trusted-host mirrors.aliyun.com
2.永久信任源
修改pip配置文件,路径见上文所述。
[install]
trusted-host=pypi.douban.com