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

Python3.8 Lambda项目本地调用报错:无法导入DEPRECATED_SERVICE_NAMES

Python 3.8 Lambda本地调用出现botocore导入错误

我维护一个基于Python 3.8的Lambda项目,执行以下命令时出现导入错误:

sam local invoke LambdaFunction --event events/get_users.json

错误日志如下:

Invoking main.lambda_handler (python3.8)
arn:aws:lambda:sa-east-1:061977363137:layer:pyodbc-layer-3_8:1 is already cached. Skipping download
arn:aws:lambda:sa-east-1:061977363137:layer:my-api-libs:3 is already cached. Skipping download
Local image is up-to-date
Using local image: samcli/lambda-python:3.8-x86_64-25626f11905fabd722693f3a7.

Mounting C:\Projects\my-api\api-main\.aws-sam\build\LambdaFunction as /var/task:ro,delegated, inside runtime container
START RequestId: 71de187e-ce8d-4fb0-9ee1-58b5d578b330 Version: $LATEST
LAMBDA_WARNING: Unhandled exception. The most likely cause is an issue in the function code. However, in rare cases, a Lambda runtime update can cause unexpected function behavior. For functions using managed runtimes, runtime updates can be triggered by a function change, or can be applied automatically. To determine if the runtime has been updated, check the runtime version in the INIT_START log entry. If this error correlates with a change in the runtime version, you may be able to mitigate this error by temporarily rolling back to the previous runtime version. For more information, see https://docs.aws.amazon.com/lambda/latest/dg/runtimes-update.html
[ERROR] Runtime.ImportModuleError: Unable to import module 'main': cannot import name 'DEPRECATED_SERVICE_NAMES' from 'botocore.docs' (/opt/python/botocore/docs/__init__.py)
Traceback (most recent call last):
END RequestId: 3df868d8-6059-49c0-b401-e6d20754a442
REPORT RequestId: 3df868d8-6059-49c0-b401-e6d20754a442  Init Duration: 0.16 ms  Duration: 638.28 ms     Billed Duration: 639 ms Memory Size: 128 MB     Max Memory Used: 128 MB
{"errorMessage": "Unable to import module 'main': cannot import name 'DEPRECATED_SERVICE_NAMES' from 'botocore.docs' (/opt/python/botocore/docs/__init__.py)", "errorType": "Runtime.ImportModuleError", "stackTrace": []}

我已升级本地Python环境的boto3和botocore至最新版本:

PS C:\Users\UserGuest\AppData\Local\Programs\Python\Python38> python.exe -m pip install --upgrade boto3
Requirement already satisfied: boto3 in c:\users\UserGuest\appdata\local\programs\python\python38\lib\site-packages (1.34.81)
Requirement already satisfied: botocore<1.35.0,>=1.34.81 in c:\users\UserGuest\appdata\local\programs\python\python38\lib\site-packages (from boto3) (1.34.81)
Requirement already satisfied: jmespath<2.0.0,>=0.7.1 in c:\users\UserGuest\appdata\roaming\python\python38\site-packages (from boto3) (1.0.1)
Requirement already satisfied: s3transfer<0.11.0,>=0.10.0 in c:\users\UserGuest\appdata\local\programs\python\python38\lib\site-packages (from boto3) (0.10.1)
Requirement already satisfied: python-dateutil<3.0.0,>=2.1 in c:\users\UserGuest\appdata\roaming\python\python38\site-packages (from botocore<1.35.0,>=1.34.81->boto3) (2.9.0.post0)
Requirement already satisfied: urllib3<1.27,>=1.25.4 in c:\users\UserGuest\appdata\roaming\python\python38\site-packages (from botocore<1.35.0,>=1.34.81->boto3) (1.26.18)
Requirement already satisfied: six>=1.5 in c:\users\UserGuest\appdata\roaming\python\python38\site-packages (from python-dateutil<3.0.0,>=2.1->botocore<1.35.0,>=1.34.81->boto3) (1.16.0)

但错误依然存在,以下是可行的解决思路:

  • 检查代码导入逻辑:DEPRECATED_SERVICE_NAMES在新版botocore中已被移除,找到代码中(或依赖库中)从botocore.docs导入该变量的位置,替换为botocore当前版本支持的替代逻辑,或升级相关依赖库以适配新版botocore。

  • 排查Lambda层依赖冲突:日志显示加载了my-api-libs层,该层可能包含旧版本botocore,与项目依赖版本冲突。需更新该层内的botocore至兼容版本,或在函数的requirements.txt中明确指定依赖版本,确保打包时使用一致的依赖。

  • 同步项目依赖版本:本地全局Python的依赖不会自动同步到SAM构建的函数包中。在项目根目录的requirements.txt中明确指定boto3和botocore版本:

    boto3==1.34.81
    botocore==1.34.81
    

    然后执行sam build重新构建函数包,再进行本地调用测试。

  • 清理SAM缓存:缓存的层或镜像可能残留旧版本依赖,执行sam local invoke --skip-pull-image --no-cache强制重新加载,或删除.aws-sam目录后重新构建。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 00:34:58