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

Python3.11部署Lambda函数遇formatargspec导入错误求助

解决Python3.11 + Serverless Lambda部署的formatargspec导入错误

问题根源

Python 3.11已正式移除inspect.formatargspec(该函数在3.10版本已被标记为废弃),而你当前使用的numpy版本仍依赖这个废弃函数,加上Serverless打包过程中可能引入了未适配Python3.11的numpy版本,导致Lambda运行时触发Runtime.ImportModuleError。

解决方案

1. 升级numpy到兼容Python3.11的版本

  • 执行命令升级本地numpy:
    pip install --upgrade numpy>=1.23.0
    
    (numpy 1.23.0及以上版本已移除对inspect.formatargspec的依赖,适配Python3.11)
  • 更新项目根目录的requirements.txt,添加或修改为:
    numpy>=1.23.0
    

2. 修复Serverless打包配置

  • 确保serverless-python-requirements插件正确处理依赖,避免本地环境差异导致的旧版本依赖打包:
    在serverless.yml中添加以下配置:
    custom:
      pythonRequirements:
        dockerizePip: true  # 在Lambda兼容的容器环境中安装依赖,保证兼容性
        useStaticCache: false  # 禁用缓存,强制重新安装依赖
    
  • 清理旧打包缓存后重新部署:
    serverless remove
    rm -rf .serverless
    serverless deploy
    

3. 临时兼容方案(无法升级numpy时使用)

如果暂时无法升级numpy,可在Lambda入口文件最顶部添加兼容补丁:

import inspect
from inspect import getfullargspec

def formatargspec(*args, **kwargs):
    # 模拟旧版formatargspec的核心功能
    if 'formatvalue' in kwargs:
        kwargs.pop('formatvalue')
    return inspect.ArgSpec(*args)._format(**kwargs)

# 给inspect模块补上缺失的函数
if not hasattr(inspect, 'formatargspec'):
    inspect.formatargspec = formatargspec

内容的提问来源于stack exchange,提问作者Alessandra Nuñez

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 15:57:15