在使用pyinstaller打包含有python-docx包的软件时出现以下错误:Failled to execute script XXX due to unhandled exception: cannot import name _elementpath
经过查询资料后发现原来是lxml这个文件没有打包进文件,所以在使用pyinstaller时增加以下内容:
pyinstaller -F -w XXX.py --hidden-import lxml._elementpath
这样再次打包时就不会出错了。本人使用的是python 3.80, 有网友测试使用 python 3.9.6 打包后不会出现同样的错误。所以要想不出错,就用高版本的python
参考文章:python - pyinstaller: ImportError: cannot import name _elementpath - Stack Overflow