在Vercel部署Django应用时遇到Python运行时配置错误
问题场景
部署Django应用到Vercel时触发以下错误:
WARN! Due to
buildsexisting in your configuration file, the Build and Development Settings defined in your Project Settings will not apply.
Installing Builder: @ardnt/vercel-python-wsgi
Starting build
Build AMI version: Amazon Linux release 2 (Karoo)
ERROR Invalid runtime configured (python3.6). Available runtimes:
ERROR - python3.9
Error: Invalid runtime configured (python3.6).
当前使用的vercel.json配置:
{ "builds": [{ "src": "Portfolio/wsgi.py", "use": "@vercel/python", "config": { "maxLambdaSize": "15mb", "runtime": "python3.9" } }], "routes": [ { "src": "/(.*)", "dest": "Portfolio/wsgi.py" } ] }
wsgi.py内容:
import os from django.core.wsgi import get_wsgi_application os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Portfolio.settings') application = get_wsgi_application() app = application
问题原因
尽管vercel.json中明确指定了python3.9,但Vercel仍读取到了python3.6的配置,通常是以下原因:
- 项目根目录存在
runtime.txt文件,内容为python3.6 - Vercel项目环境变量中设置了
PYTHON_VERSION=3.6 - 旧构建缓存残留了过时的配置
解决方案
检查
runtime.txt文件:
若项目根目录有该文件,将内容改为python-3.9(注意格式为python-<版本号>,而非python3.9);若没有则无需创建。清理环境变量:
进入Vercel项目的「Settings」→「Environment Variables」,查找是否存在PYTHON_VERSION变量,如有则删除或修改为3.9。强制清理构建缓存:
在Vercel部署页面点击「Redeploy」,勾选「Redeploy with clean cache」选项,重新触发构建。验证构建依赖:
确认使用的@ardnt/vercel-python-wsgi构建器版本为最新,避免版本兼容问题导致的配置读取异常。
内容的提问来源于stack exchange,提问作者Neveel

