解决pyinstaller 打包后运行exe程序出现的"ModuleNotFindError"

作者博客地址:http://www.yooongchun.cn/

在用pyinstaller打包python程序
pyinstaller版本为:3.3.1
python 版本:3.6.1
系统版本:Windows-10-10.0.16299-SP0


我的python程序名为:data_processing_gui.py
程序在python环境下正常运行。
打包使用的格式为:pyinstaller -F -p <导入的包路径> myprogram.py
完整打包命令为:

 pyinstaller -F -p C:\Users\fanyu\Anaconda3\Lib;C:\Users\fanyu\Anaconda3\Lib\site-packages;C:\Users\fanyu\Anaconda3\Lib\site-packages\PyQt5\Qt\bin;C:\Users\fanyu\Anaconda3\Lib\site-packages\pydicom;C:\Users\fanyu\Anaconda3\Lib\site-packages\radiomics;C:\Users\fanyu\Anaconda3\Lib\site-packages\pywt;C:\Users\fanyu\Anaconda3\Lib\site-packages\pywt\_extensions;C:\Users\fanyu\Anaconda3\Lib\site-packages\tensorflow;C:\Users\fanyu\Anaconda3\Lib\site-packages\SimpleITK;C:\Users\fanyu\Anaconda3\Lib\site-packages\PIL;C:\Users\fanyu\Anaconda3\Lib\site-packages\pandas;C:\Users\fanyu\Anaconda3\Lib\site-packages\nrrd;C:\Users\fanyu\Anaconda3\Lib\site-packages\numpy -i C:\Users\fanyu\Desktop\work\package\image\doctor.ico data_processing_gui.py
pause

打包过程无报错,打包完成后运行程序提示出错信息:

 Traceback (most recent call last):
  File "data_processing_gui.py", line 12, in <module>
  File "c:\users\fanyu\anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\radiomics\__init__.py", line 15, in <module>
  File "c:\users\fanyu\anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\radiomics\imageoperations.py", line 6, in <module>
  File "c:\users\fanyu\anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\pywt\__init__.py", line 17, in <module>
  File "c:\users\fanyu\anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\pywt\_functions.py", line 17, in <module>
  File "c:\users\fanyu\anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 714, in load_module
    module = loader.load_module(fullname)
  File "pywt/_extensions/_pywt.pyx", line 1, in init pywt._extensions._pywt (pywt\_extensions\_pywt.c:31281)
ModuleNotFoundError: No module named 'pywt._extensions._cwt'
[7564] Failed to execute script data_processing_gui

最后在Stackoverflow上发现了一个类似问题,最终解决了问题
解决方案是:

首先,使用pyinstaller 打包生成".spec" 文件

接下来,使用文本编辑器打开".spec"文件,在 hiddenimport=[] 部分添加缺少的module,我的程序中缺少的是 pywt 模块,因而添加:

hiddenimport=[
"pywt","pywt._extensions._cwt"]

然后使用pyinstaller 生成新的exe程序:

pyinstaller my.spec

最终,问题解决。
这里有Stackoverflow 上我的问题:https://stackoverflow.com/questions/49232117/using-pyinstaller-to-package-python-program-and-there-is-a-nomodulefinderror/49289859#49289859
这里是找到答案的位置:https://stackoverflow.com/questions/47607315/modulenotfounderror-no-module-named-pandas-libs-tslibs-timedeltas

©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页