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

使用Poetry的Django应用部署Vercel遇多类问题求助

解决Vercel部署Django应用时的Poetry兼容问题

问题1:修改vercel.json后提示找不到django模块

出现Runtime.ImportModuleError: No module named 'django'的核心原因是:Vercel的Python构建器默认仅读取requirements.txt安装依赖,仅设置POETRY_VERSION环境变量不会触发Poetry的依赖安装流程,导致Django等依赖未被正确部署。

解决方案:手动导出requirements.txt并指定构建脚本

  1. 在项目根目录创建build.sh脚本:
#!/bin/bash
# 安装指定版本的Poetry
pip install poetry==1.1.4
# 将Poetry依赖导出为Vercel可识别的requirements.txt
poetry export --without-hashes -f requirements.txt --output requirements.txt
  1. 给脚本添加执行权限:
chmod +x build.sh
  1. 修改vercel.json,新增command字段指定构建脚本:
{
  "build": {
    "env": {
      "POETRY_VERSION": "1.1.4"
    },
    "command": "./build.sh"
  },
  "builds": [
    {
      "src": "test/wsgi.py",
      "use": "@vercel/python",
      "config": {
        "maxLambdaSize": "15mb", 
        "runtime": "python3.9"
      }
    }
  ],
  "routes": [
    {
      "src": "/(.*)",
      "dest": "test/wsgi.py"
    }
  ]
}
  1. 将build.sh和修改后的vercel.json提交到代码仓库,重新触发Vercel构建。

问题2:使用vercel-poetry时的构建失败与OpenSSL版本冲突

poetry export命令失败、urllib3与OpenSSL版本不兼容的问题,根源是Vercel的Python 3.9环境自带OpenSSL 1.0.2k,而urllib3 v2.0要求最低OpenSSL 1.1.1+,同时vercel-poetry工具与当前环境存在兼容问题。

解决方案二选一:

方案A:降级urllib3适配Python 3.9环境

在pyproject.toml中添加urllib3的版本限制:

[tool.poetry.dependencies]
# 保留原有依赖,新增以下配置
urllib3 = "<2.0"

执行poetry lock更新依赖锁文件后重新尝试构建。

方案B:升级Python runtime到3.10+

Vercel的Python 3.10及以上版本环境自带的OpenSSL版本满足urllib3 v2.0要求,修改vercel.json中的runtime字段:

{
  "builds": [
    {
      "src": "test/wsgi.py",
      "use": "@vercel/python",
      "config": {
        "maxLambdaSize": "15mb", 
        "runtime": "python3.10"
      }
    }
  ]
}

同时推荐放弃vercel-poetry工具,改用问题1中的手动导出方案,避免第三方工具带来的额外兼容风险。

额外注意事项

  • 确保pyproject.toml中已正确声明Django等所有生产依赖,无遗漏。
  • 可使用vercel build在本地测试构建流程,提前排查问题,减少远程构建试错次数。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 14:10:15