在安装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)
从错误信息来看,你在安装 pywinpty
包时遇到了问题。具体原因是 Rust 编译过程中缺少 link.exe
,这是 Microsoft Visual C++ (MSVC) 的链接器。这表明你的系统缺少必要的 Visual C++ 构建工具。
按照deekseek给的解决步骤,需要安装安装 Visual C++ 构建工具:
pywinpty
是一个依赖于 Rust 和 C++ 的包,需要 MSVC 构建工具来编译。- 需要安装 Visual Studio Build Tools 或 Visual Studio,并确保安装了 C++ 构建工具。
步骤如下:
-
下载并安装 Visual Studio Build Tools。
-
在安装过程中,选择 C++ 生成工具(确保勾选以下组件):
- MSVC v143 - VS 2022 C++ x64/x86 生成工具
- Windows 10 SDK
- C++ CMake 工具
-
完成安装后,重启你的计算机。
-
验证
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
- 打开 Anaconda Prompt 或命令提示符,运行以下命令:
-
安装了但是未添加到路径中:
- 如果
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
- 如果
-
重新尝试安装 :
- 在 Anaconda Prompt 中激活你的
d2l-env
环境:conda activate d2l-env
- 重新运行安装命令:
pip install d2l-0.17.6-py3-none-any.whl
- 在 Anaconda Prompt 中激活你的
最终,安装成功了