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

PyInstaller打包exe报错ImportError: Module 'pythoncom' isn't in frozen sys.path如何解决

PyInstaller打包win32com相关报错解决方案

该报错核心原因为PyInstaller打包时未完整捕获pywin32库的底层依赖模块,仅添加win32com的隐藏导入无法覆盖全部所需依赖,可按以下步骤修复:

  • 先删除此前打包生成的build、dist文件夹和后缀为.spec的缓存文件,避免旧缓存干扰后续打包流程
  • 使用包含完整隐藏导入的命令打包,命令如下:
pyinstaller --onefile --hidden-import win32com --hidden-import win32com.client --hidden-import pythoncom --hidden-import pywintypes main.py
  • 若上述命令打包后的exe仍触发相同报错,可手动添加pywin32的核心动态库:
    首先找到本地Python环境下的Lib\site-packages\pywin32_system32文件夹,该目录下存在pythoncom3xx.dll、pywintypes3xx.dll两个文件(xx对应你使用的Python版本号,例如Python3.10对应310),在打包命令中新增--add-binary参数导入这两个文件即可,示例命令(请将路径替换为你本地的实际路径):
pyinstaller --onefile --hidden-import win32com --hidden-import win32com.client --hidden-import pythoncom --hidden-import pywintypes --add-binary "C:\Python310\Lib\site-packages\pywin32_system32\pythoncom310.dll;." --add-binary "C:\Python310\Lib\site-packages\pywin32_system32\pywintypes310.dll;." main.py
  • 如果你使用虚拟环境运行项目,请确认打包时调用的PyInstaller是当前虚拟环境内安装的版本,避免全局PyInstaller与虚拟环境依赖不匹配导致的依赖捕获失败。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 15:09:04