pywt `_ctw` 模块上的 Pyinstaller ImportError

Pyinstaller ImportError on pywt `_ctw` module

当使用 Pyinstaller 编译程序时,Pywt 没有正确导入 _cwt 模块。我验证了 _cwt.py 存在于我的 pywt 根目录中(在路径的站点包中)并且 _cwt.pyd 存在于 pywt\_extensions 目录中。我可以从 Python 成功导入 pywt。以下是说明 ImportError 回溯的最小(非)工作示例。

计划pywt_test.py

# -*- coding: utf-8 -*-
try:
    import sys, traceback
    import pywt
    print pywt.__version__
except ImportError:
    type_, value_, traceback_ = sys.exc_info()
    e_msg = traceback.format_exception(type_, value_, traceback_)
    with open('pywt_error_log.txt','w') as f:
        f.write(''.join(e_msg))

Pyinstaller 规范文件pywt_test.spec

 # -*- mode: python -*-

 block_cipher = None


 a = Analysis(['pywt_test.py'],
         pathex=['C:\Users\user', 'C:\Users\user'],
         binaries=[],
         datas=[],
         hiddenimports=[],
         hookspath=[],
         runtime_hooks=[],
         excludes=[],
         win_no_prefer_redirects=False,
         win_private_assemblies=False,
         cipher=block_cipher)
 pyz = PYZ(a.pure, a.zipped_data,
         cipher=block_cipher)
 exe = EXE(pyz,
      a.scripts,
      a.binaries,
      a.zipfiles,
      a.datas,
      name='pywt_test',
      debug=False,
      strip=False,
      upx=False,
      console=True)

Pyinstall编译命令:pyinstaller pywt_test.spec.

命令运行:pywt_test.exe

pywt_error_log.txt 的内容:

Traceback (most recent call last):
  File "pywt_test.py", line 10, in <module>
  File "c:\users\user\appdata\local\temp\pip-build-3zvqo7\pyinstaller\PyInstaller\loader\pyimod03_importers.py", line 389, in load_module
  File "site-packages\pywt\__init__.py", line 16, in <module>
  File "c:\users\user\appdata\local\temp\pip-build-3zvqo7\pyinstaller\PyInstaller\loader\pyimod03_importers.py", line 546, in load_module
  File "pywt\_extensions\_pywt.pyx", line 1, in init pywt._extensions._pywt (pywt\_extensions\_pywt.c:32588)
 ImportError: No module named _cwt

我试过在pathex,hiddenimports等中添加_cwtNone修改错误

如何获取 _cwt 和整个 pywt 包,以使用 Pyinstaller 加载?

版本,供参考:

只需将其添加到隐藏的导入中即可:

 ...
 hiddenimports=['pywt._extensions._cwt'],
 ...

正如 wedesoft 提到的,添加隐藏的导入是有效的。为避免将来出现此类错误,您可以添加一个文件

'\PyInstaller\hooks\hook-pywt.py'

与字符串:

hiddenimports=['pywt._extensions._cwt']

我只是拿了一个现有的文件,如“\PyInstaller\hooks\hook-patsy.py”,将 hiddenimports 行更改为上面的行,并保存为 hook-pywt.py。在您更新 PyInstaller 之前,这应该有效。