【终极解决办法】pyinstaller打包exe没有错误,运行exe提示Failed to execute script 'mainlmageWindows' due tounhandled exception: No module named 'docx'-LMLPHP

一、通过pyinstaller打包exe可执行文件,由于我的py是多个,所以要先生成spec文件,代码如下:

pyi-makespec mainImageWindows.py

此时生产了一个mainImageWindows.spec的文件,内容如下:

【终极解决办法】pyinstaller打包exe没有错误,运行exe提示Failed to execute script 'mainlmageWindows' due tounhandled exception: No module named 'docx'-LMLPHP

 我们需要修改里边的内容

【终极解决办法】pyinstaller打包exe没有错误,运行exe提示Failed to execute script 'mainlmageWindows' due tounhandled exception: No module named 'docx'-LMLPHP

 二、通过pyinstaller打包spec文件为exe文件

pyinstaller -F mainImageWindows.spec 

打包完成后会增加_pycache_、build、dist三个文件,其中exe文件在dist文件内

【终极解决办法】pyinstaller打包exe没有错误,运行exe提示Failed to execute script 'mainlmageWindows' due tounhandled exception: No module named 'docx'-LMLPHP

 【终极解决办法】pyinstaller打包exe没有错误,运行exe提示Failed to execute script 'mainlmageWindows' due tounhandled exception: No module named 'docx'-LMLPHP

 双击运行后报错,如下图:

【终极解决办法】pyinstaller打包exe没有错误,运行exe提示Failed to execute script 'mainlmageWindows' due tounhandled exception: No module named 'docx'-LMLPHP

 在网上查了好多资料后,都说是pyinstaller无法打包docx,此时我们需要找到pyinstaller包的安装位置,解决办法如下:

1、先这pycharm当中查看pyinstaller包安装的位置

【终极解决办法】pyinstaller打包exe没有错误,运行exe提示Failed to execute script 'mainlmageWindows' due tounhandled exception: No module named 'docx'-LMLPHP

 【终极解决办法】pyinstaller打包exe没有错误,运行exe提示Failed to execute script 'mainlmageWindows' due tounhandled exception: No module named 'docx'-LMLPHP

 2、在PyInstaller\hooks目录下新建一个名为“hook-docx.py”的文件

【终极解决办法】pyinstaller打包exe没有错误,运行exe提示Failed to execute script 'mainlmageWindows' due tounhandled exception: No module named 'docx'-LMLPHP

 3、在“hook-docx.py”的文件内新增以下代码:

from PyInstaller.utils.hooks import collect_data_files
datas = collect_data_files("docx")

4、然后再次pyinstaller -F mainImageWindows.spec 打包还是报同样错误

5、接着查看pyinstaller包对应的参数设置,参数如下:

【终极解决办法】pyinstaller打包exe没有错误,运行exe提示Failed to execute script 'mainlmageWindows' due tounhandled exception: No module named 'docx'-LMLPHP

 6、再次查看spec文件对应的参数,参数如下:

【终极解决办法】pyinstaller打包exe没有错误,运行exe提示Failed to execute script 'mainlmageWindows' due tounhandled exception: No module named 'docx'-LMLPHP

 【终极解决办法】pyinstaller打包exe没有错误,运行exe提示Failed to execute script 'mainlmageWindows' due tounhandled exception: No module named 'docx'-LMLPHP

 7、根据上边的参数修改了spec文件的hookspath参数为D:\anaconda\envs\zhaco工具\Lib\site-packages\PyInstaller\hooks,设置如下:

【终极解决办法】pyinstaller打包exe没有错误,运行exe提示Failed to execute script 'mainlmageWindows' due tounhandled exception: No module named 'docx'-LMLPHP

 8、此时再次打包后运行不在报Failed to execute script 'mainlmageWindows' due tounhandled exception: No module named 'docx'错误了,但是又报

 【终极解决办法】pyinstaller打包exe没有错误,运行exe提示Failed to execute script 'mainlmageWindows' due tounhandled exception: No module named 'docx'-LMLPHP

 9、此时再次更改spec文件,在hiddenimports中添加'lxml._elementpath'参数

【终极解决办法】pyinstaller打包exe没有错误,运行exe提示Failed to execute script 'mainlmageWindows' due tounhandled exception: No module named 'docx'-LMLPHP

 10、然后最后再次打包,打包代码如下:

pyinstaller -F -w mainImageWindows.spec --hidden-import lxml._elementpath

11、此时再次运行exe文件后不再报错,完没解决。

12-02 22:18