You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

部署Django机器学习API到Vercel时遇Cargo未安装错误求助

问题:Django API部署Vercel时pip安装依赖失败(Cargo未安装)

基于Python 3.9开发的集成机器学习模型的Django API,部署到Vercel平台时执行pip3.9安装依赖失败,报错提示Cargo(Rust包管理器)未安装或不在PATH中,相关依赖需要Rust和Cargo编译扩展。

部署报错日志

Error: Command failed: pip3.9 install --disable-pip-version-check --target . --upgrade -r /vercel/path0/requirements.txt
  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
BUILD_FAILED: Command failed: pip3.9 install --disable-pip-version-check --target . --upgrade -r /vercel/path0/requirements.txt  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.Collecting anyio==3.6.2  Downloading anyio-3.6.2-py3-none-any.whl (80 kB)     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 80.6/80.6 kB 14.2 MB/s eta 0:00:00Collec

依赖列表(requirements.txt)

anyio==3.6.2
argon2-cffi==21.3.0
argon2-cffi-bindings==21.2.0
asgiref==3.6.0
asttokens==2.1.0
attrs==22.1.0
backcall==0.2.0
beautifulsoup4==4.11.1
bleach==5.0.1
cffi==1.15.1
cmake==3.24.3
colorama==0.4.6
contourpy==1.0.6
cycler==0.11.0
debugpy==1.6.3
decorator==5.1.1
defusedxml==0.7.1
Django==4.1.7
django-cors-headers==3.14.0
django-environ==0.10.0
dlib==19.24.0
entrypoints==0.4
executing==1.2.0
fastjsonschema==2.16.2
fonttools==4.38.0
idna==3.4
ipykernel==6.17.1
ipython==8.6.0
ipython-genutils==0.2.0
ipywidgets==8.0.2
jedi==0.18.2
Jinja2==3.1.2
joblib==1.2.0
jsonschema==4.17.1
jupyter==1.0.0
jupyter-console==6.4.4
jupyter-server==1.23.3
jupyter_client==7.4.7
jupyter_core==5.0.0
jupyterlab-pygments==0.2.2
jupyterlab-widgets==3.0.3
kiwisolver==1.4.4
MarkupSafe==2.1.1
matplotlib==3.6.2
matplotlib-inline==0.1.6
mistune==2.0.4
nbclassic==0.4.8
nbclient==0.7.0
nbconvert==7.2.5
nbformat==5.7.0
nest-asyncio==1.5.6
notebook==6.5.2
notebook_shim==0.2.2
numpy==1.23.5
packaging==21.3
pandas==1.5.2
pandocfilters==1.5.0
parso==0.8.3
pickleshare==0.7.5
Pillow==9.3.0
platformdirs==2.5.4
prometheus-client==0.15.0
prompt-toolkit==3.0.33
psutil==5.9.4
pure-eval==0.2.2
pycparser==2.21
Pygments==2.13.0
pyparsing==3.0.9
pyrsistent==0.19.2
python-dateutil==2.8.2
pytz==2022.6
pypiwin32==223
pywinpty==2.0.9
pyzmq==24.0.1
qtconsole==5.4.0
QtPy==2.3.0
scikit-learn==1.2.1
scipy==1.9.3
Send2Trash==1.8.0
six==1.16.0
sniffio==1.3.0
soupsieve==2.3.2.post1
sqlparse==0.4.3
stack-data==0.6.1
terminado==0.17.0
threadpoolctl==3.1.0
tinycss2==1.2.1
tornado==6.2
traitlets==5.5.0
tzdata==2022.7
wcwidth==0.2.5
webencodings==0.5.1
websocket-client==1.4.2
widgetsnbextension==4.0.3

解决方案

1. 在Vercel构建流程中安装Rust工具链

修改Vercel的构建命令,在执行pip安装前先安装Rust和Cargo:

  • 进入Vercel项目的Settings → Build & Development Settings
  • 将Build Command替换为:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y && source $HOME/.cargo/env && pip3.9 install --disable-pip-version-check --target . --upgrade -r requirements.txt && python manage.py collectstatic --noinput

或者在项目根目录创建vercel.json文件,配置构建命令:

{
  "buildCommand": "curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y && source $HOME/.cargo/env && pip3.9 install --disable-pip-version-check --target . --upgrade -r requirements.txt && python manage.py collectstatic --noinput"
}

2. 清理不必要的依赖

你的requirements.txt包含大量开发环境依赖(如jupyter、ipython、notebook系列包),部署API时完全不需要这些依赖,移除它们可以减少编译压力和依赖体积:

  • 删除以下依赖:
    asttokens, backcall, debugpy, ipykernel, ipython, ipython-genutils, ipywidgets, jedi, jupyter, jupyter-console, jupyter-server, jupyter_client, jupyter_core, jupyterlab-pygments, jupyterlab-widgets, matplotlib-inline, nbclassic, nbclient, nbconvert, nbformat, nest-asyncio, notebook, notebook_shim, pickleshare, prompt-toolkit, pure-eval, qtconsole, QtPy, Send2Trash, stack-data, terminado, widgetsnbextension
    

3. 优先使用预编译轮子(可选)

对于需要编译的依赖(如dlib),可以尝试使用预编译的manylinux版本,避免在Vercel上编译:

  • 调整dlib版本为支持预编译的版本,或者在pip安装命令中添加--only-binary :all:参数强制使用预编译包。

内容的提问来源于stack exchange,提问作者Abhishek Maurya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 23:42:12