AWS CodeBuild构建失败:无法安装Python 3.8运行时
AWS CodeBuild无法安装Python 3.8的解决方案
问题说明
构建任务中通过buildspec.yml指定Python 3.8作为运行时,但构建失败,报错提示当前使用的构建镜像仅预装了Python 3.10,不支持3.8版本:
[Container] 2023/07/06 11:54:17 Selecting 'python' runtime version '3.8' based on manual selections... [Container] 2023/07/06 11:54:17 Phase complete: DOWNLOAD_SOURCE State: FAILED [Container] 2023/07/06 11:54:17 Phase context status code: YAML_FILE_ERROR Message: Unknown runtime version named '3.8' of python. This build image has the following versions: 3.10
核心原因
AWS CodeBuild的Python运行时版本支持取决于你选择的构建镜像,不同镜像预装的Python版本不同。你当前使用的镜像未包含Python 3.8,因此无法通过runtime-versions直接指定。
解决方法
方法1:更换支持Python 3.8的官方构建镜像
选择预装了Python 3.8的CodeBuild标准镜像,例如Amazon Linux 2标准镜像(aws/codebuild/amazonlinux2-x86_64-standard:4.0)。
操作步骤:
- 进入AWS CodeBuild控制台,打开目标构建项目
- 点击「编辑」→「环境」
- 在「镜像」选项中选择上述镜像版本
- 保存配置后重新触发构建
此时原buildspec.yml无需修改,runtime-versions: {python: "3.8"}可正常生效。
方法2:手动在当前镜像中安装Python 3.8
如果无法更换镜像,可在install阶段通过命令手动安装Python 3.8,修改后的buildspec.yml如下:
{ "version": "0.2", "phases": { "install": { "commands": [ "sudo amazon-linux-extras install python3.8 -y", "sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1", "python --version", "curl https://bootstrap.pypa.io/get-pip.py | python" ] }, "build": { "commands": [ "cd lambda", "python -m pip install -r requirements.txt", "python -m pip install -r test/requirements.txt", "python -m unittest" ] } }, "artifacts": { "files": "**/*" } }
说明:
- 通过
amazon-linux-extras安装Python 3.8 - 使用
update-alternatives将系统默认Python版本切换为3.8 - 重新安装pip以匹配Python 3.8版本
内容的提问来源于stack exchange,提问作者aspdeepak
相关产品推荐
相关产品推荐

