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

PyInstaller打包Tkinter+Bleak程序出现winrt模块缺失错误求助

解决PyInstaller打包bleak库时winrt.windows.foundation.collections缺失问题

核心原因

bleak在Windows平台依赖winrt模块实现BLE通信,但PyInstaller的默认钩子未自动收集winrt的部分子模块,导致打包后运行时触发模块缺失错误。

具体解决步骤

  • 方法1:打包时显式指定隐藏依赖
    执行打包命令时,通过--hidden-import参数手动添加缺失的模块,建议一次性添加bleak依赖的核心winrt子模块:

    pyinstaller --hidden-import winrt.windows.foundation.collections --hidden-import winrt.windows.foundation --hidden-import winrt.windows.devices.bluetooth --hidden-import winrt.windows.devices.bluetooth.genericattributeprofile ble.py
    
  • 方法2:通过spec文件手动配置依赖

    1. 先生成spec配置文件:
      pyinstaller --name ble ble.py
      
    2. 打开生成的ble.spec,找到hiddenimports字段,补充缺失的winrt子模块:
      a = Analysis(
          ['ble.py'],
          ...
          hiddenimports=[
              'winrt.windows.foundation.collections',
              'winrt.windows.foundation',
              'winrt.windows.devices.bluetooth',
              'winrt.windows.devices.bluetooth.genericattributeprofile'
          ],
          ...
      )
      
    3. 使用spec文件重新打包:
      pyinstaller ble.spec
      
  • 方法3:升级依赖版本
    旧版本的bleak或PyInstaller可能存在钩子不完善的问题,先升级到最新版:

    pip install --upgrade bleak pyinstaller
    
  • 验证方案
    写一个极简的bleak测试脚本(如下),用上述方法打包运行,确认问题是否解决:

    from bleak import BleakScanner
    import asyncio
    
    async def scan_devices():
        devices = await BleakScanner.discover()
        for device in devices:
            print(f"设备:{device.name},地址:{device.address}")
    
    asyncio.run(scan_devices())
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 02:18:12