RuntimeError: Couldn’t install torch 原因与解决方法

今天,一位群友在用Python开发的AI绘画程序“Stable Diffusion”的时候,结果报错如下:

RuntimeError: Couldn't install torch.

提示:Python 运行时抛出了一个异常。请检查疑难解答页面。

Command: "C:\Users\74561\Desktop\sd-webui-aki-v4\py310\python.exe" -m pip install torch==1.13.1+cu117 torchvision==0.14.1+cu117 --extra-index-url https://download.pytorch.org/whl/cu117

Error code: 2

--------

[程序崩溃,退出代码为 1 (0x00000001)]

中文翻译

运行时错误:无法安装torch。

 

原因

虽然说上面报错也说出了一些原因,但是,具体是什么原因我们还不知道?因此,我向这位群友要来了“控制台”整个调试代码,如下:

Python 3.10.11 (tags/v3.10.11:7d4cc5a, Apr  5 2023, 00:38:17) [MSC v.1929 64 bit (AMD64)]
Commit hash: 22bcc7be428c94e9408f589966c2040187245d81
Installing torch and torchvision
Looking in indexes: https://mirrors.cloud.tencent.com/pypi/simple, https://download.pytorch.org/whl/cu117
Looking in links: https://mirror.sjtu.edu.cn/pytorch-wheels/torch_stable.html
Collecting torch==1.13.1+cu117
  Downloading https://download.pytorch.org/whl/cu117/torch-1.13.1%2Bcu117-cp310-cp310-win_amd64.whl (2255.4 MB)
ERROR: Exception:
Traceback (most recent call last):
  File "C:\Users\74561\Desktop\sd-webui-aki-v4\py310\lib\site-packages\pip\_vendor\urllib3\response.py", line 438, in _error_catcher
    yield
  File "C:\Users\74561\Desktop\sd-webui-aki-v4\py310\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\74561\Desktop\sd-webui-aki-v4\py310\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\74561\Desktop\sd-webui-aki-v4\py310\lib\site-packages\pip\_vendor\cachecontrol\filewrapper.py", line 90, in read
    data = self.__fp.read(amt)
  File "http\client.py", line 466, in read
  File "socket.py", line 705, in readinto
  File "ssl.py", line 1274, in recv_into
  File "ssl.py", line 1130, in read
TimeoutError: The read operation timed out
提示:Python 运行时抛出了一个异常。请检查疑难解答页面。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\74561\Desktop\sd-webui-aki-v4\py310\lib\site-packages\pip\_internal\cli\base_command.py", line 160, in exc_logging_wrapper
    status = run_func(*args)
  File "C:\Users\74561\Desktop\sd-webui-aki-v4\py310\lib\site-packages\pip\_internal\cli\req_command.py", line 247, in wrapper
    return func(self, options, args)
  File "C:\Users\74561\Desktop\sd-webui-aki-v4\py310\lib\site-packages\pip\_internal\commands\install.py", line 419, in run
    requirement_set = resolver.resolve(
  File "C:\Users\74561\Desktop\sd-webui-aki-v4\py310\lib\site-packages\pip\_internal\resolution\resolvelib\resolver.py", line 92, in resolve
    result = self._result = resolver.resolve(
  File "C:\Users\74561\Desktop\sd-webui-aki-v4\py310\lib\site-packages\pip\_vendor\resolvelib\resolvers.py", line 481, in resolve
    state = resolution.resolve(requirements, max_rounds=max_rounds)
  File "C:\Users\74561\Desktop\sd-webui-aki-v4\py310\lib\site-packages\pip\_vendor\resolvelib\resolvers.py", line 348, in resolve
    self._add_to_criteria(self.state.criteria, r, parent=None)
  File "C:\Users\74561\Desktop\sd-webui-aki-v4\py310\lib\site-packages\pip\_vendor\resolvelib\resolvers.py", line 172, in _add_to_criteria
    if not criterion.candidates:
  File "C:\Users\74561\Desktop\sd-webui-aki-v4\py310\lib\site-packages\pip\_vendor\resolvelib\structs.py", line 151, in __bool__
    return bool(self._sequence)
  File "C:\Users\74561\Desktop\sd-webui-aki-v4\py310\lib\site-packages\pip\_internal\resolution\resolvelib\found_candidates.py", line 155, in __bool__
    return any(self)
  File "C:\Users\74561\Desktop\sd-webui-aki-v4\py310\lib\site-packages\pip\_internal\resolution\resolvelib\found_candidates.py", line 143, in <genexpr>
    return (c for c in iterator if id(c) not in self._incompatible_ids)
  File "C:\Users\74561\Desktop\sd-webui-aki-v4\py310\lib\site-packages\pip\_internal\resolution\resolvelib\found_candidates.py", line 47, in _iter_built
    candidate = func()
  File "C:\Users\74561\Desktop\sd-webui-aki-v4\py310\lib\site-packages\pip\_internal\resolution\resolvelib\factory.py", line 206, in _make_candidate_from_link
    self._link_candidate_cache[link] = LinkCandidate(
  File "C:\Users\74561\Desktop\sd-webui-aki-v4\py310\lib\site-packages\pip\_internal\resolution\resolvelib\candidates.py", line 297, in __init__
    super().__init__(
  File "C:\Users\74561\Desktop\sd-webui-aki-v4\py310\lib\site-packages\pip\_internal\resolution\resolvelib\candidates.py", line 162, in __init__
    self.dist = self._prepare()
  File "C:\Users\74561\Desktop\sd-webui-aki-v4\py310\lib\site-packages\pip\_internal\resolution\resolvelib\candidates.py", line 231, in _prepare
    dist = self._prepare_distribution()
  File "C:\Users\74561\Desktop\sd-webui-aki-v4\py310\lib\site-packages\pip\_internal\resolution\resolvelib\candidates.py", line 308, in _prepare_distribution
    return preparer.prepare_linked_requirement(self._ireq, parallel_builds=True)
  File "C:\Users\74561\Desktop\sd-webui-aki-v4\py310\lib\site-packages\pip\_internal\operations\prepare.py", line 491, in prepare_linked_requirement
    return self._prepare_linked_requirement(req, parallel_builds)
  File "C:\Users\74561\Desktop\sd-webui-aki-v4\py310\lib\site-packages\pip\_internal\operations\prepare.py", line 536, in _prepare_linked_requirement
    local_file = unpack_url(
  File "C:\Users\74561\Desktop\sd-webui-aki-v4\py310\lib\site-packages\pip\_internal\operations\prepare.py", line 166, in unpack_url
    file = get_http_url(
  File "C:\Users\74561\Desktop\sd-webui-aki-v4\py310\lib\site-packages\pip\_internal\operations\prepare.py", line 107, in get_http_url
    from_path, content_type = download(link, temp_dir.path)
  File "C:\Users\74561\Desktop\sd-webui-aki-v4\py310\lib\site-packages\pip\_internal\network\download.py", line 147, in __call__
    for chunk in chunks:
  File "C:\Users\74561\Desktop\sd-webui-aki-v4\py310\lib\site-packages\pip\_internal\cli\progress_bars.py", line 53, in _rich_progress_bar
    for chunk in iterable:
  File "C:\Users\74561\Desktop\sd-webui-aki-v4\py310\lib\site-packages\pip\_internal\network\utils.py", line 63, in response_chunks
    for chunk in response.raw.stream(
  File "C:\Users\74561\Desktop\sd-webui-aki-v4\py310\lib\site-packages\pip\_vendor\urllib3\response.py", line 622, in stream
    data = self.read(amt=amt, decode_content=decode_content)
  File "C:\Users\74561\Desktop\sd-webui-aki-v4\py310\lib\site-packages\pip\_vendor\urllib3\response.py", line 560, in read
    with self._error_catcher():
  File "contextlib.py", line 153, in __exit__
  File "C:\Users\74561\Desktop\sd-webui-aki-v4\py310\lib\site-packages\pip\_vendor\urllib3\response.py", line 443, in _error_catcher
    raise ReadTimeoutError(self._pool, None, "Read timed out.")
pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='download.pytorch.org', port=443): Read timed out.
Traceback (most recent call last):
  File "C:\Users\74561\Desktop\sd-webui-aki-v4\launch.py", line 355, in <module>
    prepare_environment()
  File "C:\Users\74561\Desktop\sd-webui-aki-v4\launch.py", line 257, in prepare_environment
    run(f'"{python}" -m {torch_command}', "Installing torch and torchvision", "Couldn't install torch", live=True)
  File "C:\Users\74561\Desktop\sd-webui-aki-v4\launch.py", line 81, in run
    raise RuntimeError(f"""{errdesc or 'Error running command'}.
RuntimeError: Couldn't install torch.
提示:Python 运行时抛出了一个异常。请检查疑难解答页面。
Command: "C:\Users\74561\Desktop\sd-webui-aki-v4\py310\python.exe" -m pip install torch==1.13.1+cu117 torchvision==0.14.1+cu117 --extra-index-url https://download.pytorch.org/whl/cu117
Error code: 2

--------

[程序崩溃,退出代码为 1 (0x00000001)]

 

经过分析,我总算知道具体的原因了,主要就是:

 

1、这“Stable Diffusion”程序在执行以下命令:

C:\Users\74561\Desktop\sd-webui-aki-v4\py310\python.exe -m pip install torch==1.13.1+cu117 torchvision==0.14.1+cu117 --extra-index-url https://download.pytorch.org/whl/cu117

的时候。

 

2、去下载:

https://download.pytorch.org/whl/cu117/torch-1.13.1%2Bcu117-cp310-cp310-win_amd64.whl

的时候。

 

3、由于网络原因,造成了:

TimeoutError: The read operation timed out

中文翻译

超时错误:读取操作超时

 

解决方法

知道了原因,解决起来就容易多了,具体操作步骤如下:

 

1、确保浏览器能打开网址:

https://download.pytorch.org/whl/cu117/torch-1.13.1%2Bcu117-cp310-cp310-win_amd64.whl

结果:正常。

 

2、cmd命令窗口执行命令。

以我这为例子,我叫他参考“PyTorch下载+安装(pip安装Torch)教程”,执行如下命令:

C:\Users\74561\Desktop\sd-webui-aki-v4\py310\python.exe -m pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117

结果:提示“远程主机强迫关闭了一个现有的连接”。

download.pytorch.org远程主机强迫关闭了一个现有的连接

这是因为:

  • 网络不稳定:如果网络环境不好,数据包的传输可能会出现错误,导致连接中断。
  • 超时:如果连接时间太长,可能会超出远程主机的超时限制,从而导致连接中断。

 

3、从而断定,浏览器可以访问网址,cmd命令窗口下不能以命令的方式来访问。

因此最终的解决方法只有用“加速器”或“VPN”,用梯子来实现科学上网。

 

注意:

由于我们这里用的是stable-diffusion,并不推荐大家用下面这另一种方法去解决,因为此程序后面还会下载很多.whl的文件,手工一个一个的去操作太麻烦了。不如改变网络状态一劳永逸舒服得多。

 

还有另一种解决此问题的方法:

 

1、浏览器打开网址:

https://download.pytorch.org/whl/cu117/torch-1.13.1%2Bcu117-cp310-cp310-win_amd64.whl

并下载.whl文件。

 

2、如果我们有一个whl文件,可以使用pip install命令来安装whl, 例如:

pip install some_package.whl

这里如果有不懂的,具体请参考:pip安装本地whl文件(pip离线安装whl)教程

 

3、以这位群友为例子,那么,应该执行如下命令来安装whl:

C:\Users\74561\Desktop\sd-webui-aki-v4\py310\python.exe pip install torch-1.13.1%2Bcu117-cp310-cp310-win_amd64.whl
付杰
  • ¥ 79.0元
  • 市场价:99.0元
  • ¥ 69.0元
  • 市场价:69.0元
  • ¥ 98.0元
  • 市场价:198.0元
  • ¥ 99.0元
  • 市场价:99.0元

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: