使用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
相关产品推荐
相关产品推荐

