当使用Pyinstaller编译程序时,Pywt没有正确导入_cwt
模块。我确认我的pywt根目录中存在_cwt.py
(路径中的站点包中),而_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 spec文件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等。无更改错误。
如何使用Pyinstaller加载_cwt
和整个pywt包?
版本,供参考:
答案 0 :(得分:3)
只需将其添加到隐藏的导入中:
...
hiddenimports=['pywt._extensions._cwt'],
...
答案 1 :(得分:1)
如 wedesoft 所述,添加隐藏的导入有效。为了避免将来出现此类错误,您可以添加文件
'\ PyInstaller \ hooks \ hook-pywt.py'
带有字符串:
hiddenimports=['pywt._extensions._cwt']
我只是简单地获取一个现有文件,例如'\ PyInstaller \ hooks \ hook-patsy.py',将hiddenimports行更改为上面的文件,并保存为hook-pywt.py。在您更新PyInstaller之前,它应该一直起作用。