推送到Heroku时提示Cargo未安装或不在PATH,如何解决?
Heroku推送Python应用编译失败解决方案
错误日志详情
remote: Preparing metadata (pyproject.toml): started remote: Preparing metadata (pyproject.toml): finished with status 'error' remote: error: subprocess-exited-with-error remote: remote: × Preparing metadata (pyproject.toml) 运行失败。 remote: │ exit code: 1 remote: ╰─> [6 lines of output] remote: Checking for Rust toolchain... remote: remote: Cargo, the Rust package manager, is not installed or is not on PATH. remote: 该包需要Rust和Cargo来编译扩展,请通过系统包管理器或访问https://rustup.rs/进行安装。 remote: remote: [end of output] remote: remote: note: 此错误源自子进程,大概率与pip无关。 remote: error: metadata-generation-failed remote: remote: × 生成包元数据时遇到错误。 remote: ╰─> 详见上述输出。 remote: remote: note: 这是上述包的问题,而非pip的问题。 remote: hint: 详见上述详情。 remote: ! Push被拒绝,Python应用编译失败。 remote: remote: ! Push失败
问题原因
你的Python应用依赖的某个包需要Rust工具链(包含Cargo包管理器)编译扩展,但Heroku默认的Python构建环境未预装Rust,导致编译流程中断。
解决方法
方法一:添加Rust构建包(Buildpack)
Heroku支持多构建包串联执行,先安装Rust环境再构建Python应用:
- 方式1:在项目根目录创建
.buildpacks文件,写入以下内容:https://github.com/heroku/heroku-buildpack-rust.git https://github.com/heroku/heroku-buildpack-python.git - 方式2:通过Heroku命令行工具设置:
heroku buildpacks:add --index 1 https://github.com/heroku/heroku-buildpack-rust.git heroku buildpacks:add --index 2 https://github.com/heroku-buildpack-python.git
设置完成后重新推送代码即可。
方法二:替换为无需编译的包
检查requirements.txt或pyproject.toml中的依赖包,确认是否有纯Python版本或预编译的wheel包可用,替换后再推送应用。
内容的提问来源于stack exchange,提问作者ASUVATH M S
相关产品推荐
相关产品推荐

