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

在Vercel部署Django应用时出现构建错误,请求排查

在Vercel部署Django应用时构建失败的排查求助

按照视频教程的方法在Vercel上部署Django应用,但构建过程报错,无法定位原因,求熟悉该部署流程的人帮忙排查。以下是相关日志与配置文件:

日志

WARN! Due to `builds` existing in your configuration file, the Build and Development Settings defined in your Project Settings will not apply. Learn More: https://vercel.link/unused-build-settings
Installing required dependencies...
Failed to run "pip3.9 install --disable-pip-version-check --target . --upgrade -r /vercel/path0/requirements.txt"
Error: Command failed: pip3.9 install --disable-pip-version-check --target . --upgrade -r /vercel/path0/requirements.txt
  error: subprocess-exited-with-error
  
  × Getting requirements to build wheel did not run successfully.
  │ exit code: 1
  ╰─> [21 lines of output]
      running egg_info
      writing psycopg2.egg-info/PKG-INFO
      writing dependency_links to psycopg2.egg-info/dependency_links.txt
      writing top-level names to psycopg2.egg-info/top_level.txt
      
      Error: pg_config executable not found.
      
      pg_config is required to build psycopg2 from source.  Please add the directory
      containing pg_config to the $PATH or specify the full executable path with the
      option:
      
          python setup.py build_ext --pg-config /path/to/pg_config build ...
      
      or with the pg_config option in 'setup.cfg'.
      
      If you prefer to avoid building psycopg2 from source, please install the PyPI
      'psycopg2-binary' package instead.
      
      For further information please check the 'doc/src/install.rst' file (also at
      <https://www.psycopg.org/docs/install.html>).
      
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

requirements.txt

asgiref==3.8.1
beautifulsoup4==4.12.3
crispy-bootstrap5==2024.2
Django==4.2.11
django-bootstrap-v5==1.0.11
django-crispy-forms==2.1
django-tinymce==3.7.1
gunicorn==21.2.0
packaging==24.0
pillow==10.2.0
psycopg2==2.9.9
psycopg2-binary==2.9.9
soupsieve==2.5
sqlparse==0.4.4
tzdata==2024.1

settings.py

ALLOWED_HOSTS = ['127.0.0.1', 'localhost', '.vercel.app', '.now.sh']
STATIC_ROOT = BASE_DIR / "staticfiles"

vercel.json

{
    "version": 2,
    "builds": [
      {
        "src": "core/wsgi.py",
        "use": "@vercel/python",
        "config": { "maxLambdaSize": "15mb", "runtime": "python3.9" }
      }],
    "routes": [
      {
        "src": "/(.*)",
        "dest": "core/wsgi.py"
      }
    ]
  }

build_files.sh

pip install -r requirements.txt

python3.9 manage.py collectstatic --noinput

问题分析与解决方案

核心原因

构建失败是因为psycopg2包需要PostgreSQL的系统编译工具pg_config,但Vercel的Python构建环境默认没有安装PostgreSQL组件,无法从源码编译安装该包。

解决步骤

  1. 清理冗余依赖
    编辑requirements.txt,删除psycopg2==2.9.9这一行,只保留psycopg2-binary==2.9.9。psycopg2-binary是预编译的二进制包,无需编译环境即可直接安装,完全满足Django连接PostgreSQL的需求。

  2. 统一pip版本
    修改build_files.sh中的安装命令,指定使用Python3.9对应的pip,避免环境版本不一致问题:

    pip3.9 install -r requirements.txt
    python3.9 manage.py collectstatic --noinput
    
  3. 重新触发构建
    将修改后的代码提交到仓库,让Vercel重新执行构建流程即可。

额外提示

如果你的Django应用需要连接PostgreSQL数据库,直接使用托管式PostgreSQL服务(如Supabase、AWS RDS等)即可,无需在Vercel构建环境中安装本地PostgreSQL。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 05:35:22