安装torch报错 raise ReadTimeoutError(self._pool, None, “Read timed out.“) pip._vendor.urllib3.exceptions

1. 配置cuda的torch环境时报错

1. 配置命令

pip install torch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2 --index-url https://download.pytorch.org/whl/cu118 

2.报错bug

pip install torch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2 --index-url https://download.pytorch.org/whl/cu118
Looking in indexes: https://download.pytorch.org/whl/cu118
Collecting torch==2.0.1
  Downloading https://download.pytorch.org/whl/cu118/torch-2.0.1%2Bcu118-cp310-cp310-win_amd64.whl (2619.1 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╸━━━━━━━━━━ 1.9/2.6 GB 1.3 MB/s eta 0:08:55
ERROR: Exception:
Traceback (most recent call last):
  File "C:\Users\bjr\.conda\envs\deeplivecam\lib\site-packages\pip\_vendor\urllib3\response.py", line 438, in _error_catcher
    yield
  File "C:\Users\bjr\.conda\envs\deeplivecam\lib\site-packages\pip\_vendor\urllib3\response.py", line 561, in read
    data = self._fp_read(amt) if not fp_closed else b""
  File "C:\Users\bjr\.conda\envs\deeplivecam\lib\site-packages\pip\_vendor\urllib3\response.py", line 527, in _fp_read
    return self._fp.read(amt) if amt is not None else self._fp.read()
  File "C:\Users\bjr\.conda\envs\deeplivecam\lib\site-packages\pip\_vendor\cachecontrol\filewrapper.py", line 98, in read
    data: bytes = self.__fp.read(amt)
  File "C:\Users\bjr\.conda\envs\deeplivecam\lib\http\client.py", line 465, in read
    s = self.fp.read(amt)
  File "C:\Users\bjr\.conda\envs\deeplivecam\lib\socket.py", line 705, in readinto
    return self._sock.recv_into(b)
  File "C:\Users\bjr\.conda\envs\deeplivecam\lib\ssl.py", line 1274, in recv_into
    return self.read(nbytes, buffer)
  File "C:\Users\bjr\.conda\envs\deeplivecam\lib\ssl.py", line 1130, in read
    return self._sslobj.read(len, buffer)
TimeoutError: The read operation timed out

During handling of the above exception, another exception occurred:
......
    raise ReadTimeoutError(self._pool, None, "Read timed out.")
pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='download.pytorch.org', port=443): Read timed out.

这个错误提示说明在下载 torch 软件包时发生了超时错误,可能是由于网络连接不稳定、下载速度慢或者服务器暂时不可用导致的。要解决这个问题,你可以尝试以下几种方法:
增加下载超时时间: 你可以通过设置 --timeout 参数来增加下载的超时时间,比如设置为 1000 秒:

2.解决方法

1.增加下载超时时间:

你可以通过设置 --timeout 参数来增加下载的超时时间,比如设置为 1000 秒:

 pip install torch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2 --index-url https://download.pytorch.org/whl/cu118 --timeout 1000

2.尝试使用镜像源:

你可以尝试使用国内的 PyPI 镜像源,这些镜像通常能提供更快的下载速度。比如使用清华大学的镜像:

pip install torch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2 --index-url https://pypi.tuna.tsinghua.edu.cn/simple

3.检查网络连接:

确保你的网络连接稳定,可以尝试切换到更快的网络,或者关闭 VPN 等可能影响网络连接的工具。

4.分次安装:

如果一次性安装多个软件包出现问题,可以尝试分别安装每个软件包。例如:

pip install torch==2.0.1 --index-url https://download.pytorch.org/whl/cu118
pip install torchvision==0.15.2 --index-url https://download.pytorch.org/whl/cu118
pip install torchaudio==2.0.2 --index-url https://download.pytorch.org/whl/cu118

5.重试安装:

有时候,网络问题可能是暂时的,稍后重新尝试安装可能就会成功。

6.手动下载 .whl 文件安装

如果以上方法都没有解决问题,可以考虑手动下载 .whl 文件,然后使用 pip install .whl 命令进行安装。

### 解决 PyTorch 安装时的 ReadTimeoutError 问题 在 Python 环境下安装 PyTorch 时常会遇到 `ReadTimeoutError` 的超时错误,这通常是由于网络连接不稳定或镜像源速度较慢引起的。以下是几种有效的解决方案来应对这一问题。 #### 方法一:更换国内镜像源 通过指定国内镜像源可以显著提高下载速度并减少超时的可能性。例如,使用清华大学开源软件镜像站作为 pip 镜像源: ```bash pip install torch torchvision torchaudio --index-url https://pypi.tuna.tsinghua.edu.cn/simple ``` 这种方法能够有效缓解因国外服务器访问缓慢而导致的超时问题[^1]。 #### 方法二:利用 Anaconda 进行安装 Anaconda 提供了一个更稳定的环境用于管理依赖库。可以通过 conda 渠道直接安装 PyTorch: ```bash conda install pytorch torchvision torchaudio cpuonly -c pytorch ``` 如果仍然遭遇超时,则可尝试切换到清华 TUNA 或中科大等国内 anaconda 镜像源配置文件路径下的 `.condarc` 文件设置如下内容以加速下载过程[^2]: ```yaml channels: - defaults show_channel_urls: true default_channels: - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r custom_channels: conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud ``` 完成上述更改之后再执行命令即可。 #### 方法三:离线安装 当在线方式始终无法成功解决问题时,可以选择手动下载 whl 文件后再进行本地安装。具体操作步骤是从官方支持页面找到对应版本链接地址后,在浏览器里打开它并将目标文件保存至本机硬盘上;接着运行以下指令完成加载工作流程[^3]: ```bash pip install /path/to/downloaded/file.whl ``` 以上三种途径均能不同程度地帮助克服由网络状况引发的各种异常情况,从而顺利完成所需框架工具集部署任务。 ### 注意事项 确保所选方案中的各组件版本相互兼容非常重要,否则可能会引入新的冲突或者不稳定性风险。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值