问题:
pyinstall打包后出现win32comgenpy有关报错,本地py文件运行没有问题,打包后无法运行。
Traceback (most recent call last):
File “PyInstaller\hooks\rthooks\pyi_rth_win32comgenpy.py”, line 46, in
File “PyInstaller\hooks\rthooks\pyi_rth_win32comgenpy.py”, line 25, in pyi_rthook
File “PyInstaller\loader\pyimod02_importers.py”, line 385, in exec_module
File "win32com_init.py", line 6, in
File “PyInstaller\loader\pyimod02_importers.py”, line 385, in exec_module
File “pythoncom.py”, line 3, in
File “pywintypes.py”, line 61, in import_pywin32_system_module
ImportError: Module ‘pythoncom’ isn’t in frozen sys.path [‘C:\Users\Weiyang\AppData\Local\Temp\_MEI71722\base_library.zip’, ‘C:\UsTemp\_MEI71722\lib-dynload’, ‘C:\Users\Weiyang\AppData\Local\Temp\_MEI71722’, ‘C:\Users\Weiyang\AppData\Local\Temp\_MEI71722\AppData\Local\Temp\_MEI71722\pythonwin’, ‘C:\Users\Weiyang\AppData\Local\Temp\_MEI71722\pywin32_system32’]
[6416] Failed to execute script ‘pyi_rth_win32comgenpy’ due to unhandled exception!
解决思路:
1.增加 --hidden-import “pywin32” 来忽略pywin32的导入(不生效进行后续操作)
pyinstaller -F test.py --hidden-import "pywin32"
2.以管理员模式打开conda shell,在此窗口pip uninstall pywin32,再执行conda install pywin32(原因可能是不知道什么时候安的pywin32与conda环境冲突)
参考链接:
有多种解决方案,比如说降级pyinstaller版本,纯pip install所有包等等,本人尝试后上面的方法最有效
https://github.com/brentvollebregt/auto-py-to-exe/issues/46
https://github.com/mhammond/pywin32/issues/1634
https://stackoverflow.com/questions/65587443/failed-to-execute-script-pyi-rth-win32comgenpy-after-packing-with-pyinstaller
https://stackoverflow.com/questions/69799910/failed-to-execute-script-pyi-rth-win32comgenpy-due-to-unhandled-exception-mod
pyinstaller打包使用方法:
懒人式教程:https://zhuanlan.zhihu.com/p/367063488
详细原理式教程:https://blog.csdn.net/qq_48979387/article/details/132359366
传递参数教程:https://blog.csdn.net/fendouwhy/article/details/73245689