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

本地运行Python脚本时awsglue模块导入异常求助

问题描述

本地Python 3.12.9(MacOS 15.3.1 M2 Pro)环境中,导入internal_lib.py时出现异常。该文件包含以下代码:

try:
    from awsglue.utils import getResolvedOptions
except (ImportError, ModuleNotFoundError) as e:
    pass

执行import internal_lib时,即便已明确捕获ModuleNotFoundError,仍报错:

Exception has occurred: ModuleNotFoundError
No module named 'awsglue'
  File "/Users/user/repos/repo/functions/internal_lib.py", line 18, in <module>
    from awsglue.utils import getResolvedOptions
  File "/Users/xx/repos/repo/functions/internal_lib.py", line 19, in <module>
    import internal_lib
ModuleNotFoundError: No module named 'awsglue'

直接运行上述4行代码也会触发相同错误。需求为无需注释代码即可忽略该异常,或实现awsglue的本地安装。

解决方法

方法一:修复循环导入与异常捕获逻辑

从报错栈可以看到,internal_lib.py第19行存在import internal_lib语句,这会导致循环导入:模块在加载过程中又触发自身导入,此时try-except块还未完全生效,无法捕获awsglue的导入异常。

  • 移除internal_lib.py中第19行的import internal_lib语句,消除循环导入。
  • 确保from awsglue.utils import getResolvedOptions语句完全被包裹在try-except块内,无其他代码打断异常捕获流程。

方法二:本地适配awsglue依赖

awsglue是AWS Glue专属运行环境的库,官方未提供PyPI安装包,可通过以下两种方式本地适配:

  • 模拟awsglue模块:在项目根目录创建awsglue/utils.py文件,写入模拟实现:
    def getResolvedOptions(args, options):
        # 本地模拟逻辑,可根据需求返回对应值
        return {}
    
    Python导入时会优先加载本地模拟模块,避免触发导入异常。
  • 复制AWS Glue环境的库文件:若能访问AWS Glue运行环境,复制/opt/python/lib/python3.x/site-packages/awsglue整个文件夹到本地项目的Python路径(如项目根目录或Python的site-packages目录)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 00:42:13