You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CentOS 7虚拟环境安装Django报pip ReadTimeout错误排查

问题描述

报错截图
在CentOS 7的venv虚拟环境中安装Django时触发报错,无法定位诱因。系统共存两个Python版本:通过系统包管理器安装的Python 3.6.8,后续自行安装的Python 3.10.5,报错信息调用的是Python 3.6.8路径,不确定双版本是否为故障原因。
完整报错日志如下:

Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/pip/_vendor/urllib3/response.py", line 302, in _error_catcher
    yield
  File "/usr/lib/python3.6/site-packages/pip/_vendor/urllib3/response.py", line 384, in read
    data = self._fp.read(amt)
  File "/usr/lib/python3.6/site-packages/pip/_vendor/cachecontrol/filewrapper.py", line 60, in read
    data = self.__fp.read(amt)
  File "/usr/lib64/python3.6/http/client.py", line 459, in read
    n = self.readinto(b)
  File "/usr/lib64/python3.6/http/client.py", line 503, in readinto
    n = self.fp.readinto(b)
  File "/usr/lib64/python3.6/socket.py", line 586, in readinto
    return self._sock.recv_into(b)
  File "/usr/lib64/python3.6/ssl.py", line 971, in recv_into
    return self.read(nbytes, buffer)
  File "/usr/lib64/python3.6/ssl.py", line 833, in read
    return self._sslobj.read(len, buffer)
  File "/usr/lib64/python3.6/ssl.py", line 590, 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/lib/python3.6/site-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/usr/lib/python3.6/site-packages/pip/commands/install.py", line 346, in run
    requirement_set.prepare_files(finder)
  File "/usr/lib/python3.6/site-packages/pip/req/req_set.py", line 381, in prepare_files
    ignore_dependencies=self.ignore_dependencies))
  File "/usr/lib/python3.6/site-packages/pip/req/req_set.py", line 623, in _prepare_file
    session=self.session, hashes=hashes)
  File "/usr/lib/python3.6/site-packages/pip/download.py", line 821, in unpack_url
    hashes=hashes
  File "/usr/lib/python3.6/site-packages/pip/download.py", line 659, in unpack_http_url
    hashes)
  File "/usr/lib/python3.6/site-packages/pip/download.py", line 882, in _download_http_url
    _download_url(resp, link, content_file, hashes)
  File "/usr/lib/python3.6/site-packages/pip/download.py", line 603, in _download_url
    hashes.check_against_chunks(downloaded_chunks)
  File "/usr/lib/python3.6/site-packages/pip/utils/hashes.py", line 46, in check_against_chunks
    for chunk in chunks:
  File "/usr/lib/python3.6/site-packages/pip/download.py", line 571, in written_chunks
    for chunk in chunks:
  File "/usr/lib/python3.6/site-packages/pip/utils/ui.py", line 139, in iter
    for x in it:
  File "/usr/lib/python3.6/site-packages/pip/download.py", line 560, in resp_read
    decode_content=False):
  File "/usr/lib/python3.6/site-packages/pip/_vendor/urllib3/response.py", line 436, in stream
    data = self.read(amt=amt, decode_content=decode_content)
  File "/usr/lib/python3.6/site-packages/pip/_vendor/urllib3/response.py", line 401, in read
    raise IncompleteRead(self._fp_bytes_read, self.length_remaining)
  File "/usr/lib64/python3.6/contextlib.py", line 99, in __exit__
    self.gen.throw(type, value, traceback)
  File "/usr/lib/python3.6/site-packages/pip/_vendor/urllib3/response.py", line 307, in _error_catcher
    raise ReadTimeoutError(self._pool, None, 'Read timed out.')
pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.
故障判定
  • 核心报错为ReadTimeoutError,是pip从默认PyPI源下载Django安装包时网络连接超时导致,和系统共存两个Python版本无直接关联。
  • 报错路径显示Python 3.6.8,是因为创建venv虚拟环境时,默认关联了系统自带的Python 3.6.8解释器,未指定自行安装的Python 3.10.5版本。
解决方法
  • 处理网络超时问题:
    安装时为pip指定可正常访问的PyPI镜像源,同时调大超时阈值,命令格式如下:
    pip install django -i 本地可访问的PyPI镜像地址 --default-timeout=100
    如需永久配置镜像源,执行:
    pip config set global.index-url 本地可访问的PyPI镜像地址
  • 如需使用Python 3.10.5创建虚拟环境:
    创建venv时明确指定Python 3.10的解释器路径,命令格式如下:
    python3.10 -m venv 自定义虚拟环境目录名
    激活该虚拟环境后再执行安装操作,就会调用Python 3.10对应的pip,不会再出现3.6版本路径。

注意:CentOS 7部分底层系统工具依赖自带的Python 3.6运行,不要随意卸载该版本,避免触发系统功能异常。

内容的提问来源于stack exchange,提问作者Pau00005

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.27 03:09:18