PyInstaller打包的Python可执行文件调用报错求助
解决PyInstaller打包Python3.12可执行文件的ModuleNotFoundError问题
错误原因
你遇到的ModuleNotFoundError: No module named 'ipaddress'及后续的pyimod02_importers加载错误,本质是Python3.12的模块结构变更,搭配的PyInstaller版本不兼容Python3.12,导致打包过程遗漏了必要依赖模块。
解决方案
1. 升级PyInstaller到兼容Python3.12的版本
执行命令更新PyInstaller:
pip install --upgrade pyinstaller
验证版本(需≥5.13.0,此版本开始正式支持Python3.12):
pyinstaller --version
2. 清理旧打包残留并重新打包
- 删除之前打包生成的
build文件夹、dist文件夹和dummy_fct.spec文件 - 重新执行打包命令:
pyinstaller --onefile dummy_fct.py
3. (可选)手动强制包含缺失模块
如果升级后仍有问题,打包时显式指定需要包含的ipaddress模块:
pyinstaller --onefile --hidden-import ipaddress dummy_fct.py
验证步骤
打包完成后,先在命令行直接测试可执行文件:
dummy_fct.exe PDH
确认输出正常后,再在Jupyter Notebook中调用subprocess.run代码。
内容的提问来源于stack exchange,提问作者MysteryGuy
相关产品推荐
相关产品推荐

