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

使用PyInstaller打包Python应用时遇pythoncom模块AttributeError错误

解决PyInstaller打包时AttributeError: Failed to retrieve attribute __file__ from module pythoncom错误

以下是几个可行的解决方法,按优先级尝试:

  • 显式导入并修复pythoncom的__file__属性
    在你的脚本最开头添加这段代码,手动为pythoncom模块补上缺失的__file__属性:

    import pythoncom
    if not hasattr(pythoncom, '__file__'):
        pythoncom.__file__ = 'pythoncom.py'
    

    保存脚本后重新执行打包命令:pyinstaller --onefile -w my_script.py

  • 打包时指定隐藏导入
    直接在打包命令中添加--hidden-import参数强制导入pythoncom:

    pyinstaller --onefile -w --hidden-import=pythoncom my_script.py
    
  • 修改PyInstaller的hook文件
    找到Python安装目录下的Lib\site-packages\PyInstaller\hooks文件夹,创建或编辑hook-pythoncom.py文件,写入以下内容:

    from PyInstaller.utils.hooks import collect_data_files
    
    datas = collect_data_files('pythoncom')
    hiddenimports = ['pythoncom']
    
    def pre_safe_import_module(api):
        module = api.module
        if not hasattr(module, '__file__'):
            module.__file__ = 'pythoncom.py'
    

    保存后重新执行打包操作。

  • 降级PyInstaller版本
    部分新版本PyInstaller与pythoncom存在兼容性问题,尝试降级到稳定版本:

    pip uninstall pyinstaller -y
    pip install pyinstaller==5.13.2
    

    安装完成后再执行打包命令。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 04:04:55