ModuleNotFoundError:没有名为'pyttsx3.driver'(使用pyinstaller编译的文件)的模块,但未编译时工作正常

2022-03-23 00:00:00 python pyinstaller pyttsx

问题描述

我使用pyinstaller编译程序,python文件在未编译时工作正常,但在编译&;测试时引发错误。

这是全部错误,我认为这可能是因为pyinstaller

Traceback (most recent call last):
  File "site-packagespyttsx3\__init__.py", line 20, in init
  File "c:python37libweakref.py", line 137, in __getitem__
    o = self.data[key]()
KeyError: 'sapi5'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "song_dl.py", line 25, in <module>
    engine = pyttsx3.init('sapi5')
  File "site-packagespyttsx3\__init__.py", line 22, in init
  File "site-packagespyttsx3engine.py", line 30, in __init__
  File "site-packagespyttsx3driver.py", line 50, in __init__
  File "importlib\__init__.py", line 127, in import_module
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'pyttsx3.drivers'
[1072] Failed to execute script song_dl

解决方案

查看文档的When things went wrong部分 尤其是Listing hidden imports

看起来pyinstaller无法"知道"它需要添加此特定模块,因此您需要显式指定它

可能类似于

$ pyinstaller --hidden-import=pyttsx3.drivers song_dl.py

相关文章