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

PyInstaller添加hidden-import仍报ModuleNotFoundError(addict)问题求助

问题原因及解决方法

原因分析

  1. 错误的入口文件指定方式:你使用*.py作为打包输入文件,会将当前目录下所有Python文件都当作入口脚本处理,干扰了PyInstaller的依赖分析逻辑,导致--hidden-import addict的配置未被正确关联到主程序main.py的依赖链中。
  2. 包结构解析偏差:代码位于pyinstaller_test包内,多入口打包方式让PyInstaller无法准确追踪包内导入依赖的第三方库,进而遗漏addict模块的打包。

解决方法

方法1:指定正确的单一入口脚本

修改打包命令,直接以pyinstaller_test/main.py作为唯一入口,保留--hidden-import参数:

pyinstaller --hidden-import addict --onefile --name main pyinstaller_test/main.py

方法2:使用spec文件精确控制打包

spec文件能更灵活配置打包细节,步骤如下:

  1. 生成对应main.py的spec文件:
    pyi-makespec --onefile --name main pyinstaller_test/main.py
    
  2. 打开生成的main.spec文件,找到hiddenimports字段,将addict添加到列表中:
    hiddenimports=['addict'],
    
  3. 基于spec文件执行打包:
    pyinstaller main.spec
    

方法3:让PyInstaller自动检测依赖

在myimport.py中显式添加对addict模块的完整导入,帮助PyInstaller的依赖分析工具自动识别该库:

# myimport.py
from addict import Dict
import addict  # 新增该行,触发PyInstaller的依赖检测

之后用正确的入口命令打包(无需--hidden-import):

pyinstaller --onefile --name main pyinstaller_test/main.py

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 09:32:13