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

使用Serverless+Poetry+Python3.10部署Lambda遇模块缺失问题

Serverless部署Python3.10 Lambda时FastAPI依赖未打包问题排查

编辑

问题根源指向Serverless插件serverless-python-requirements:

  • 使用$ sls package打包时,当pyproject.toml中python = "^3.9",依赖会被正常包含在zip包内
  • 但改为python = "^3.10"时,依赖完全不被打包,其余配置完全一致

问题详情

使用Serverless Framework、Poetry、Python3.10和FastAPI部署AWS Lambda时,相同操作在Python3.9环境下可正常运行,但Python3.10环境下调用端点返回{"message": "Internal server error"}。

Lambda日志报错:

[ERROR] Runtime.ImportModuleError: Unable to import module 'main': No module named 'fastapi'
Traceback (most recent call last):

核心代码(main.py)

# main.py
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from mangum import Mangum

app = FastAPI()

app.add_middleware(CORSMiddleware, allow_origins="*", allow_credentials=True, allow_methods=["*"], allow_headers=["*"])

@app.get("/api/health-check/")
def health_check():
    return {"message": "OK"}

handle = Mangum(app)

本地操作流程

  1. 初始化Poetry项目
$ poetry init
.....
Compatible Python versions [^3.9]:  3.10

Would you like to define your main dependencies interactively? (yes/no) [yes] no
Would you like to define your development dependencies interactively? (yes/no) [yes] no
Generated file
.....
[tool.poetry.dependencies]
python = "3.10"
  1. 添加开发依赖失败
$ poetry add --dev pytest pytest-cov black isort flake8 bandit
The currently activated Python version 3.9.9 is not supported by the project (3.10).
Trying to find and use a compatible version.

Poetry was unable to find a compatible version. If you have one, you can explicitly use it via the "env use" command.
  1. 本地Python版本情况
    本地安装了3个Python版本,可通过以下命令查看指定版本:
$ python3 --version
Python 3.9.9

$ python3.10 --version
Python 3.10.11
  1. 修改Python版本约束后解决依赖安装问题
    修改pyproject.toml:
[tool.poetry.dependencies]
python = "~3.10"

重新添加依赖成功:

$ poetry add --dev pytest pytest-cov black isort flake8 bandit
The currently activated Python version 3.9.9 is not supported by the project (~3.10).
Trying to find and use a compatible version.
Using python3.10 (3.10.11)
Creating virtualenv test-sls-deploy-md9kc90P-py3.10 in /Users/costin/Library/Caches/pypoetry/virtualenvs
.....

$ poetry add fastapi uvicorn httpx
The currently activated Python version 3.9.9 is not supported by the project (~3.10).
Trying to find and use a compatible version.
Using python3.10 (3.10.11)
Using version ^0.95.1 for fastapi
.....
  1. 执行部署
$ sls deploy --stage development --verbose

部署过程中收到python3.10不在预期运行环境列表中的警告,但部署完成。

Serverless配置文件(serverless.yml)

service: test-sls-deploy-api

frameworkVersion: '3'
useDotenv: true

provider:
  name: aws
  runtime: python3.10
  region: 'eu-west-3'
  stage: 'development'
  logRetentionInDays: 30

functions:
  TEST-DEPLOY:
    handler: main.handle
    memorySize: 512
    events:
      - http:
          path: /{proxy+}
          method: any
          cors:
            origin: ${env:ALLOWED_ORIGINS}
            maxAge: 60

custom:
  pythonRequirements:
    usePoetry: true
    noDeploy:
      - boto3  # already on Lambda
      - botocore  # already on Lambda

plugins:
  - serverless-python-requirements

疑问

原本预期serverless-python-requirements插件能像Python3.9环境中一样打包FastAPI依赖,但Python3.10环境下未达预期,问题出在哪里?


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 09:35:39