4

使用 Pyinstaller 编译程序时, Pywt 没有正确导入_cwt模块。我验证了_cwt.py它存在于我的 pywt 根目录中(在路径上的站点包中)并且_cwt.pydpywt\_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

我尝试添加_cwt到 pathex、hiddenimports 等。没有改变错误。

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

版本,供参考:

  • pywt:0.5.1
  • 安装程序:3.2.1
  • Python:Windows 7 64 位(Anaconda)上的 2.7.12 64 位
4

2 回答 2

7

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

 ...
 hiddenimports=['pywt._extensions._cwt'],
 ...
于 2017-05-18T12:18:59.093 回答
2

As wedesoft mentioned, adding hidden imports works. To avoid such errors in the future you might add a file

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

with the string:

hiddenimports=['pywt._extensions._cwt']

I simply took an existing file like '\PyInstaller\hooks\hook-patsy.py', changed the hiddenimports line to the one above and saved as hook-pywt.py. That should work until you update your PyInstaller.

于 2018-09-04T16:19:42.510 回答