
本文旨在解决在安装 `sentence-transformers` 等Python包时,因缺少Rust编译器及Cargo包管理器而导致的 `subprocess-exited-with-error` 错误。教程将详细指导用户通过官方 `rustup` 工具链管理器安装Rust环境,并验证其配置,最终成功完成Python包的安装,确保开发环境的完整性与稳定性。
在Python生态系统中,许多高性能的科学计算、机器学习或数据处理库为了优化性能,会集成用C/C++、Rust等底层语言编写的扩展模块。当您尝试使用 pip 安装这类Python包时,如果其依赖的Rust扩展需要编译,而您的系统环境中又缺少Rust编译器(rustc)及其包管理器(Cargo),就会出现 subprocess-exited-with-error 错误。
典型的错误信息如下所示,明确指出“Cargo, the Rust package manager, is not installed or is not on PATH.”:
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://www.php.cn/link/1c8dcf919f8a604f3a488b0e4b0f1420
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此错误表明 pip 尝试运行一个子进程来准备包的元数据,但该子进程因找不到编译Rust扩展所需的工具链而失败。解决此问题的关键在于正确安装Rust开发环境。
立即学习“Python免费学习笔记(深入)”;
Rust官方提供了一个名为 rustup 的工具链管理器,它能够简化Rust编译器的安装和管理过程,并确保所有必要的组件(包括Cargo)都被正确配置。
访问Rust官方推荐的 rustup 安装页面:https://www.php.cn/link/1c8dcf919f8a604f3a488b0e4b0f1420。 根据您的操作系统(Windows, macOS, Linux),选择对应的安装方式:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
此命令会下载并执行 rustup 安装脚本。在安装过程中,系统会提示您选择安装类型,建议选择默认的“1) Proceed with installation (default)”。
安装完成后,rustup 会自动配置环境变量,使得 rustc 和 cargo 命令在终端中可用。
安装完成后,为了确认Rust工具链已成功安装并配置到系统路径中,可以在终端中运行以下命令来检查 rustup 的版本:
rustup --version
如果命令成功执行并显示 rustup 的版本信息,例如 rustup 1.26.0 (e1ee09805 2023-09-06),则表明Rust环境已准备就绪。您也可以进一步验证 cargo 是否可用:
cargo --version
同样,如果显示 cargo 的版本信息,则说明Rust工具链已完全可用。
在确认Rust工具链已正确安装后,您可以再次尝试安装之前失败的Python包。以 sentence-transformers 为例:
pip install sentence-transformers
此时,pip 在尝试编译任何Rust扩展时,将能够找到所需的Rust编译器和Cargo,从而顺利完成包的安装。
通过遵循上述步骤,您将能够有效地解决Python包安装中与Rust依赖相关的 subprocess-exited-with-error 错误,确保您的开发环境能够顺利安装和使用依赖Rust的Python库。
以上就是解决Python包安装中Rust/Cargo依赖引发的Subprocess错误的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号