《动手学深度学习》d2l库安装失败问题总结

在安装d2l库时一直在报错,遇到的问题如下:
参考《动手学深度学习》中d2l库的安装教程(含细节,开盖即食)进行手动安装whl文件时出现了如下错误:

  输入:pip install d2l-0.17.6-py3-none-any.whl
  ......
  Preparing metadata (pyproject.toml) ... error
  error: subprocess-exited-with-error

  × Preparing metadata (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [6 lines of output]

      Cargo, the Rust package manager, is not installed or is not on PATH.
      This package requires Rust and Cargo to compile extensions. Install it through
      the system's package manager or via https://rustup.rs/

      Checking for Rust toolchain....
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.

在这里插入图片描述

从错误信息来看,d2l-0.17.6-py3-none-any.whl 这个包在安装时需要编译一些 Rust 扩展,但你的系统上没有安装 Rust 的包管理器 Cargo,因此导致安装失败。(deepseek)

检查Rust 和 Cargo 是否已安装
在终端运行以下命令:

rustc --version
cargo --version

如果显示了版本号,说明 Rust 和 Cargo 已安装。如果没有,需要先安装 Rust 和 Cargo。
安装完后,我可以在终端确认到版本号,但是在Anaconda环境中没有检测到,可能是没有添加到Anaconda中,于是将Rust 和 Cargo 的路径添加到当前环境的 PATH 中:

set PATH=%USERPROFILE%\.cargo\bin;%PATH%

接着重新安装d2l库:

(d2l-env) C:\Users\19282>pip install d2l-0.17.6-py3-none-any.whl

这次安装的东西比较多,但是还是出现了问题,甚至有乱码,报错如下:

Building wheels for collected packages: pywinpty
  Building wheel for pywinpty (pyproject.toml) ... error
  error: subprocess-exited-with-error

  × Building wheel for pywinpty (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [40 lines of output]
      Running `maturin pep517 build-wheel -i C:\Users\19282\.conda\envs\d2l-env\python.exe --compatibility off`
      鈿狅笍  Warning: `project.version` field is required in pyproject.toml unless it is present in the `project.dynamic` list
      馃摝 Including license file "C:\Users\19282\AppData\Local\Temp\pip-install-3d2g64fy\pywinpty_413e76701f8e4a9484e579ebdeb922c6\LICENSE.txt"
      馃嵐 Building a mixed python/rust project
      馃敆 Found pyo3 bindings
      馃悕 Found CPython 3.8 at C:\Users\19282\.conda\envs\d2l-env\python.exe
         Compiling windows_x86_64_msvc v0.52.6
         Compiling proc-macro2 v1.0.88
         Compiling unicode-ident v1.0.13
         Compiling target-lexicon v0.12.16
         Compiling autocfg v1.4.0
         Compiling once_cell v1.20.2
         Compiling libc v0.2.160
         Compiling winsafe v0.0.19
         Compiling either v1.13.0
         Compiling heck v0.5.0
         Compiling unindent v0.2.3
         Compiling indoc v2.0.5
         Compiling bitflags v2.6.0
         Compiling cfg-if v1.0.0
      error: linker `link.exe` not found
        |
        = note: program not found

      note: the msvc targets depend on the msvc linker but `link.exe` was not found

      note: please ensure that Visual Studio 2017 or later, or Build Tools for Visual Studio were installed with the Visual C++ option.

      note: VS Code is a different product, and is not sufficient.

      error: could not compile `windows_x86_64_msvc` (build script) due to 1 previous error
      warning: build failed, waiting for other jobs to finish...
      error: could not compile `proc-macro2` (build script) due to 1 previous error
      error: could not compile `libc` (build script) due to 1 previous error
      error: could not compile `indoc` (lib) due to 1 previous error
      error: could not compile `target-lexicon` (build script) due to 1 previous error
      馃挜 maturin failed
        Caused by: Failed to build a native library through cargo
        Caused by: Cargo build finished with "exit code: 101": `"cargo" "rustc" "--message-format" "json-render-diagnostics" "--manifest-path" "C:\\Users\\19282\\AppData\\Local\\Temp\\pip-install-3d2g64fy\\pywinpty_413e76701f8e4a9484e579ebdeb922c6\\Cargo.toml" "--release" "--lib"`
      Error: command ['maturin', 'pep517', 'build-wheel', '-i', 'C:\\Users\\19282\\.conda\\envs\\d2l-env\\python.exe', '--compatibility', 'off'] returned non-zero exit status 1
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for pywinpty
Failed to build pywinpty
ERROR: ERROR: Failed to build installable wheels for some pyproject.toml based projects (pywinpty)

error: linker  not found

从错误信息来看,你在安装 pywinpty 包时遇到了问题。具体原因是 Rust 编译过程中缺少 link.exe,这是 Microsoft Visual C++ (MSVC) 的链接器。这表明你的系统缺少必要的 Visual C++ 构建工具。

按照deekseek给的解决步骤,需要安装安装 Visual C++ 构建工具

  • pywinpty 是一个依赖于 Rust 和 C++ 的包,需要 MSVC 构建工具来编译。
  • 需要安装 Visual Studio Build ToolsVisual Studio,并确保安装了 C++ 构建工具

步骤如下

  1. 下载并安装 Visual Studio Build Tools

  2. 在安装过程中,选择 C++ 生成工具(确保勾选以下组件):

    • MSVC v143 - VS 2022 C++ x64/x86 生成工具
    • Windows 10 SDK
    • C++ CMake 工具
  3. 完成安装后,重启你的计算机。

  4. 验证 link.exe 是否可用:

    • 打开 Anaconda Prompt 或命令提示符,运行以下命令:
      where link.exe
      
    • 如果 link.exe 已正确安装,你应该会看到类似以下的输出:
      C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.36.32532\bin\Hostx64\x64\link.exe
      
  5. 安装了但是未添加到路径中:

    • 如果 link.exe 已安装但未在 PATH 中,可以手动添加其路径。
    • 找到 link.exe 的路径(通常在 C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\<版本号>\bin\Hostx64\x64)。
    • 在 Anaconda Prompt 中运行以下命令,将其添加到当前环境的 PATH
      set PATH=C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\<版本号>\bin\Hostx64\x64;%PATH%
      
    • 验证是否成功:
      where link.exe
      
  6. 重新尝试安装

    • 在 Anaconda Prompt 中激活你的 d2l-env 环境:
      conda activate d2l-env
      
    • 重新运行安装命令:
      pip install d2l-0.17.6-py3-none-any.whl
      

最终,安装成功了
在这里插入图片描述

### 解决 `pip install d2l` 出现 `subprocess-exited-with-error` 的方法 当遇到 `pip install d2l` 导致的 `subprocess-exited-with-error` 错误时,可能的原因包括依赖缺失、编译器配置不当或网络连接问题。以下是针对该错误的具体解决方案: #### 1. 验证并修复 Pip 和 Setuptools 版本 确保使用的 `pip` 和 `setuptools` 是最新版本,因为旧版本可能导致兼容性问题。 ```bash python -m pip install --upgrade pip setuptools wheel ``` 此操作会升级到最新的稳定版工具链[^1]。 #### 2. 检查系统中的 LibCurl 支持 如果运行的是基于 Conda 的环境,则需要确认是否存在必要的支持(如 `libcurl`)。可以通过以下命令验证其存在与否: ```bash conda list | grep libcurl ``` 若未发现任何匹配项,则需手动安装 `libcurl` : ```bash conda install -c conda-forge libcurl ``` 这一步骤有助于解决因缺少底层 HTTP 请求处理功能而引发的问题[^2]。 #### 3. 调整 PyPI 镜像源设置 考虑到国内访问官方 PyPI 可能较慢甚至不稳定的情况,建议切换至更可靠的镜像站点来加速下载过程。例如采用清华大学开源软件镜像服务作为替代地址: ```bash pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple ``` 完成更改之后再试一次安装命令即可。 #### 4. 使用 Verbose 参数获取更多调试信息 为了进一步定位具体失败原因,在执行安装指令时附加 `-v` 或者 `--verbose` 开启详尽日志模式: ```bash pip install d2l --verbose ``` 通过分析输出内容可以更好地理解哪里出了差错以及如何针对性调整策略。 #### 5. 尝试从 Wheel 文件直接安装 有时构建某些包所需的 C 扩展会由于目标平台差异等原因无法顺利完成。此时可以从预编译好的二进制文件入手解决问题。先查询对应版本号下的可用 wheels: https://www.lfd.uci.edu/~gohlke/pythonlibs/#d2l 接着下载适合当前系统的 `.whl` 并利用如下方式导入模块: ```bash pip install /path/to/downloaded/d2l.whl ``` 注意替换实际路径名部分以适应本地存储位置。 --- ### 示例代码片段展示 下面给出一段简单的 Python 测试脚本来加载 D2L 模块,并打印基本信息用于初步验证成功状态: ```python import d2l print(f"D2L Version Installed: {d2l.__version__}") ``` 上述程序应当正常工作而不抛异常才表明彻底解决了之前的安装障碍。 ---
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值