由于 pip 源站点部署在国外,下载速度很慢,甚至完全无法访问:
1
2
3
4
|
$ pip install flask
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x102930588>, 'Connection to pypi.org timed out. (connect timeout=15)')': /simple/flask/
WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x102930f28>, 'Connection to pypi.org timed out. (connect timeout=15)')': /simple/flask/
WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x102930cc0>, 'Connection to pypi.org timed out. (connect timeout=15)')': /simple/flask/
|
最不想看到 timeout 这样的字眼,有一种望洋兴叹的感觉……
指定镜像源
好在国内有不少公司和高校建了 pip 源镜像,可以极大地优化访问速度。
举个例子,我们可以通过 -i 选项,让 pip 命令使用阿里云提供的镜像源:
1
|
$ pip install flask -i https://mirrors.aliyun.com/pypi/simple/
|
-i 后面跟着的是阿里云 pip 镜像源的地址。
阿里云镜像源应该可以支持大部分网络环境,如果你是教育网,可以试试清华大学的。实在不行的话,你可以照着本文整理的国内镜像源列表,一个个试,总有可用的。
请注意,如果 pip 镜像源地址是 http 协议的话,你需要加 –trusted-host 选项,信任镜像源域名。否则, pip 将会报错:
1
2
3
4
5
6
7
|
$ pip install flask -i http://pypi.douban.com/simple/
Looking in indexes: http://pypi.douban.com/simple/
WARNING: The repository located at pypi.douban.com is not a trusted or secure host and is being ignored. If this repository is available via HTTPS we recommend you use HTTPS instead, otherwise you may silence this warning and allow it anyway with '--trusted-host pypi.douban.com'.
ERROR: Could not find a version that satisfies the requirement flask (from versions: none)
ERROR: No matching distribution found for flask
WARNING: You are using pip version 19.3.1; however, version 20.0.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
|
加上 –trusted-host 选项,即可解决问题:
1
|
$ pip install flask -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
|
环境变量
当我们需要执行 pip 命令安装多个包时,每次运行命令均指定镜像源未免会有些繁琐。这时,我们可以通过 环境变量 指定镜像源地址,。
以类 Unix 系统为例,先设置 PIP_INDEX_URL 环境变量:
1
|
$ export PIP_INDEX_URL=https://mirrors.aliyun.com/pypi/simple/
|
此后再执行 pip 命令时,便无须通过 -i 选项指定镜像源了:
通过 expose 设置环境变量,在当前 shell 会话有效, shell 退出则失效。
国内镜像源
【小菜学Python】系列文章首发于公众号【小菜学编程】,敬请关注: