FastAPI部署AWS Lambda报错:No module named 'dependencies'求助
FastAPI部署AWS Lambda报错:No module named 'dependencies'
操作流程
- 执行以下命令安装依赖到
dependencies目录:
pip3 install -r requirements.txt --platform manylinux2014_x86_64 --target=dependencies --implementation cp --python-version 3.12 --only-binary=:all: --upgrade openai
- 将
dependencies目录内容打包到压缩包:
(cd dependencies; zip ../aws_lambda_artifact.zip -r .)
- 添加
src目录到压缩包:
zip aws_lambda_artifact.zip -u -r src
错误信息
{ "errorMessage": "Unable to import module 'src.main': No module named 'dependencies'", "errorType": "Runtime.ImportModuleError", "requestId": "", "stackTrace": [] }
相关文件内容
main.py
from fastapi import FastAPI from mangum import Mangum from src import models from src.database import engine from src.routers import blog, user, authentication app = FastAPI() handler = Mangum(app) models.Base.metadata.create_all(bind=engine) app.include_router(blog.router) app.include_router(user.router) app.include_router(authentication.router)
requirements.txt
fastapi==0.114.2 mangum==0.17.0 SQLAlchemy==2.0.34 passlib==1.7.4 bcrypt==4.2.0 python-jose==3.3.0 python-multipart==0.0.9
完整日志
/var/task/pydantic/_internal/_config.py:341: UserWarning: Valid config keys have changed in V2: * 'orm_mode' has been renamed to 'from_attributes' warnings.warn(message, UserWarning) [ERROR] Runtime.ImportModuleError: Unable to import module 'src.main': No module named 'dependencies' Traceback (most recent call last):INIT_REPORT Init Duration: 1323.45 ms Phase: init Status: error Error Type: Runtime.Unknown /var/task/pydantic/_internal/_config.py:341: UserWarning: Valid config keys have changed in V2: * 'orm_mode' has been renamed to 'from_attributes' warnings.warn(message, UserWarning) [ERROR] Runtime.ImportModuleError: Unable to import module 'src.main': No module named 'dependencies' Traceback (most recent call last):INIT_REPORT Init Duration: 19347.21 ms Phase: invoke Status: error Error Type: Runtime.Unknown START RequestId: 6ae13d71-0d1f-428a-8793-e79ee0590f81 Version: $LATEST END RequestId: 6ae13d71-0d1f-428a-8793-e79ee0590f81 REPORT RequestId: 6ae13d71-0d1f-428a-8793-e79ee0590f81 Duration: 19376.13 ms Billed Duration: 19377 ms Memory Size: 128 MB Max Memory Used: 104 MB Status: error Error Type: Runtime.Unknown
API Gateway测试模板
{ "body": "eyJ0ZXN0IjoiYm9keSJ9", "resource": "/{proxy+}", "path": "/blogs", "httpMethod": "GET", "isBase64Encoded": true, "queryStringParameters": { "foo": "bar" }, "multiValueQueryStringParameters": { "foo": [ "bar" ] }, "pathParameters": { "proxy": "/blogs" }, "stageVariables": { "baz": "qux" }, "headers": { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", "Accept-Encoding": "gzip, deflate, sdch", "Accept-Language": "en-US,en;q=0.8", "Cache-Control": "max-age=0", "CloudFront-Forwarded-Proto": "https", "CloudFront-Is-Desktop-Viewer": "true", "CloudFront-Is-Mobile-Viewer": "false", "CloudFront-Is-SmartTV-Viewer": "false", "CloudFront-Is-Tablet-Viewer": "false", "CloudFront-Viewer-Country": "US", "Host": "1234567890.execute-api.us-east-1.amazonaws.com", "Upgrade-Insecure-Requests": "1", "User-Agent": "Custom User Agent String", "Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)", "X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==", "X-Forwarded-For": "127.0.0.1, 127.0.0.2", "X-Forwarded-Port": "443", "X-Forwarded-Proto": "https" }, "multiValueHeaders": { "Accept": [ "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" ], "Accept-Encoding": [ "gzip, deflate, sdch" ], "Accept-Language": [ "en-US,en;q=0.8" ], "Cache-Control": [ "max-age=0" ], "CloudFront-Forwarded-Proto": [ "https" ], "CloudFront-Is-Desktop-Viewer": [ "true" ], "CloudFront-Is-Mobile-Viewer": [ "false" ], "CloudFront-Is-SmartTV-Viewer": [ "false" ], "CloudFront-Is-Tablet-Viewer": [ "false" ], "CloudFront-Viewer-Country": [ "US" ], "Host": [ "0123456789.execute-api.us-east-1.amazonaws.com" ], "Upgrade-Insecure-Requests": [ "1" ], "User-Agent": [ "Custom User Agent String" ], "Via": [ "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)" ], "X-Amz-Cf-Id": [ "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==" ], "X-Forwarded-For": [ "127.0.0.1, 127.0.0.2" ], "X-Forwarded-Port": [ "443" ], "X-Forwarded-Proto": [ "https" ] }, "requestContext": { "accountId": "123456789012", "resourceId": "123456", "stage": "prod", "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef", "requestTime": "09/Apr/2015:12:34:56 +0000", "requestTimeEpoch": 1428582896000, "identity": { "cognitoIdentityPoolId": null, "accountId": null, "cognitoIdentityId": null, "caller": null, "accessKey": null, "sourceIp": "127.0.0.1", "cognitoAuthenticationType": null, "cognitoAuthenticationProvider": null, "userArn": null, "userAgent": "Custom User Agent String", "user": null }, "path": "/blogs", "resourcePath": "/{proxy+}", "httpMethod": "GET", "apiId": "1234567890", "protocol": "HTTP/1.1" } }
Amazon Q初步建议
错误表明Lambda函数无法导入'src.main'模块,原因是找不到其依赖的'dependencies'模块。这种情况通常是函数代码中模块导入缺失或指定错误导致的。
- 检查Lambda函数代码,确保'dependencies'模块已正确安装并存在于部署包中。
- 如果'dependencies'模块未包含在部署包中,更新函数代码以添加缺失模块:
- 若使用虚拟环境,安装'dependencies'模块并重新创建包含更新后虚拟环境的部署包。
- 若未使用虚拟环境,安装'dependencies'模块并将其加入部署包。
- 更新部署包后,上传新包以更新Lambda函数代码。
- 若问题仍存在,检查Lambda函数配置,确保为'src.main'模块指定了正确的处理器。
- 若问题依旧,查看Lambda函数日志以获取更多错误信息,帮助定位根本原因。
问题根源与解决方案
根源分析
你的打包操作是将dependencies目录下的所有依赖包(如fastapi、sqlalchemy)直接提取到了压缩包的根目录,而非保留dependencies文件夹。但报错显示代码在尝试导入名为dependencies的模块,说明src目录下的某个文件存在错误的导入语句(比如from dependencies import sqlalchemy或import dependencies),而不是直接导入依赖包本身(如import sqlalchemy)。
解决方案
检查并修正所有导入语句:遍历
src目录下的所有文件(包括models.py、database.py、routers下的所有文件),找到所有引用dependencies的导入语句,将其修改为直接导入对应的依赖包。例如:- 错误写法:
from dependencies.sqlalchemy import create_engine - 正确写法:
from sqlalchemy import create_engine
- 错误写法:
验证打包结构:解压
aws_lambda_artifact.zip,确认压缩包根目录包含所有依赖包文件夹(如fastapi、sqlalchemy)和src目录,没有多余的dependencies文件夹。重新打包部署:修正代码后,重新执行打包命令并上传至Lambda。
内容的提问来源于stack exchange,提问作者tail
相关产品推荐
相关产品推荐

