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

如何解决Vercel部署Django项目时的Runtime.ImportModuleError错误?

解决Vercel部署Django时的Runtime.ImportModuleError: No module named 'django'

可能的原因及修复步骤

1. 确认requirements.txt的位置与命名

Vercel的Python构建器默认读取**项目根目录下的requirements.txt**来安装依赖,请检查:

  • 文件是否准确命名为requirements.txt(全小写,无拼写错误)
  • 文件是否放置在项目根目录,而非子文件夹内

2. 修正vercel.json的Python构建配置

你的vercel.json中已指定Python构建器,可尝试显式声明依赖文件路径,确保构建器能识别:

{
    "builds": [{
        "src": "ang/wsgi.py",
        "use": "@vercel/python",
        "config": { 
            "maxLambdaSize": "15mb", 
            "runtime": "python3.9",
            "requirements": "./requirements.txt"
        }
    },
    {
      "src": "build.sh",
      "use": "@vercel/static-build",
      "config": {
        "distDir": "staticfiles"
      }
    }
    ],
    "routes": [
        {
            "src": "/static/(.*)",
            "dest": "/static/$1"
        },
        {
            "src": "/(.*)",
            "dest": "ang/wsgi.py"
        }
    ],
    "outputDirectory": "staticfiles"
}

3. 完善build.sh的依赖安装步骤

你配置了静态文件构建流程,需确保在生成静态文件前先安装依赖。修改build.sh内容:

#!/bin/bash
pip install -r requirements.txt
python manage.py collectstatic --noinput

注意:本地需执行chmod +x build.sh赋予脚本可执行权限后,再提交代码到仓库。

4. 验证wsgi.py的路径与导入配置

打开ang/wsgi.py,确认Django环境变量与导入逻辑正确:

import os
from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ang.settings')

application = get_wsgi_application()

如果settings.py不在ang文件夹下,需对应修改DJANGO_SETTINGS_MODULE的路径值。

5. 查看Vercel构建日志排查问题

在Vercel部署控制台查看构建日志,搜索Installing dependencies from requirements.txt相关输出:

  • 若没有该输出:说明Vercel未读取到requirements.txt,需重新确认文件位置与命名
  • 若有错误提示:根据提示修复依赖版本兼容等问题

内容的提问来源于stack exchange,提问作者Vitor Anghêben

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 11:02:32