[解决] pip 下载package速度太慢:更换国内pip源

文章目录

    • 前言
    • 场景1:pip install
    • 解决方案
    • 场景2: 更新pip
    • 解决方案
    • 总结

前言

如题,有时候pip install的速度实在太慢,忍不了,这里记录解决方案。

场景1:pip install

以scipy的安装为例:
pip install scipy
贼慢,10k/s,要等半小时吧估计都不一定能下载。

解决方案

pip install scipy -i https://pypi.tuna.tsinghua.edu.cn/simple 

关键在于:-i https://pypi.tuna.tsinghua.edu.cn/simple,使用的是国内源。

场景2: 更新pip

我在更新pip3的时候:运行pip3 install --upgrade pip,报错如下:

apr@apr:~/test$ pip3 install --upgrade pip
Collecting pip
  Downloading https://files.pythonhosted.org/packages/43/84/23ed6a1796480a6f1a2d38f2802901d078266bda38388954d01d3f2e821d/pip-20.1.1-py2.py3-none-any.whl (1.5MB)
    85% |███████████████████████████▌    | 1.3MB 5.1kB/s eta 0:00:42Exception:
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/pip/_vendor/requests/packages/urllib3/response.py", line 232, in _error_catcher
    yield
  File "/usr/local/lib/python3.6/site-packages/pip/_vendor/requests/packages/urllib3/response.py", line 314, in read
    data = self._fp.read(amt)
  File "/usr/local/lib/python3.6/site-packages/pip/_vendor/cachecontrol/filewrapper.py", line 60, in read
    data = self.__fp.read(amt)
  File "/usr/local/lib/python3.6/http/client.py", line 449, in read
    n = self.readinto(b)
  File "/usr/local/lib/python3.6/http/client.py", line 493, in readinto
    n = self.fp.readinto(b)
  File "/usr/local/lib/python3.6/socket.py", line 586, in readinto
    return self._sock.recv_into(b)
  File "/usr/local/lib/python3.6/ssl.py", line 1009, in recv_into
    return self.read(nbytes, buffer)
  File "/usr/local/lib/python3.6/ssl.py", line 871, in read
    return self._sslobj.read(len, buffer)
  File "/usr/local/lib/python3.6/ssl.py", line 631, in read
    v = self._sslobj.read(len, buffer)
socket.timeout: The read operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/usr/local/lib/python3.6/site-packages/pip/commands/install.py", line 324, in run
    requirement_set.prepare_files(finder)
  File "/usr/local/lib/python3.6/site-packages/pip/req/req_set.py", line 380, in prepare_files
    ignore_dependencies=self.ignore_dependencies))
  File "/usr/local/lib/python3.6/site-packages/pip/req/req_set.py", line 620, in _prepare_file
    session=self.session, hashes=hashes)
  File "/usr/local/lib/python3.6/site-packages/pip/download.py", line 821, in unpack_url
    hashes=hashes
  File "/usr/local/lib/python3.6/site-packages/pip/download.py", line 659, in unpack_http_url
    hashes)
  File "/usr/local/lib/python3.6/site-packages/pip/download.py", line 882, in _download_http_url
    _download_url(resp, link, content_file, hashes)
  File "/usr/local/lib/python3.6/site-packages/pip/download.py", line 603, in _download_url
    hashes.check_against_chunks(downloaded_chunks)
  File "/usr/local/lib/python3.6/site-packages/pip/utils/hashes.py", line 46, in check_against_chunks
    for chunk in chunks:
  File "/usr/local/lib/python3.6/site-packages/pip/download.py", line 571, in written_chunks
    for chunk in chunks:
  File "/usr/local/lib/python3.6/site-packages/pip/utils/ui.py", line 139, in iter
    for x in it:
  File "/usr/local/lib/python3.6/site-packages/pip/download.py", line 560, in resp_read
    decode_content=False):
  File "/usr/local/lib/python3.6/site-packages/pip/_vendor/requests/packages/urllib3/response.py", line 357, in stream
    data = self.read(amt=amt, decode_content=decode_content)
  File "/usr/local/lib/python3.6/site-packages/pip/_vendor/requests/packages/urllib3/response.py", line 324, in read
    flush_decoder = True
  File "/usr/local/lib/python3.6/contextlib.py", line 99, in __exit__
    self.gen.throw(type, value, traceback)
  File "/usr/local/lib/python3.6/site-packages/pip/_vendor/requests/packages/urllib3/response.py", line 237, in _error_catcher
    raise ReadTimeoutError(self._pool, None, 'Read timed out.')
pip._vendor.requests.packages.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.
You are using pip version 9.0.1, however version 20.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

由上可知,关键错误为:socket.timeout: The read operation timed out

原因在于:pip3下载速度太慢了。根本更新不了。(网络问题)

解决方案

参考:socket.timeout: The read operation timed out 更改pip源至国内镜像,显著提升下载速度

网上有很多可用的源,例如
豆瓣:http://pypi.douban.com/simple/
清华:https://pypi.tuna.tsinghua.edu.cn/simple

最近使用得比较多并且比较顺手的是清华大学的pip源,它是官网pypi的镜像,每隔5分钟同步一次,地址为
https://pypi.tuna.tsinghua.edu.cn/simple

运行:
pip3 install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple
会发现超级超级快。
秒下。包括各种pip install指令。nice。

总结

国内源还是给力的。

你可能感兴趣的:(PHD,Cand1-2)