ModuleNotFoundError: No module named 'pyttsx3.drivers' (File Compiled with pyinstaller), but working fine as uncompiled

ModuleNotFoundError: No module named 'pyttsx3.drivers' (File Compiled with pyinstaller), but working fine as uncompiled

我使用 pyinstaller 编译了我的程序,python 文件在未编译时工作正常,但在编译和测试时抛出错误。

这是完整的错误,我想这可能是因为 pyinstaller

Traceback (most recent call last):
  File "site-packages\pyttsx3\__init__.py", line 20, in init
  File "c:\python37\lib\weakref.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-packages\pyttsx3\__init__.py", line 22, in init
  File "site-packages\pyttsx3\engine.py", line 30, in __init__
  File "site-packages\pyttsx3\driver.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 无法"know"它需要添加这个特定的模块,因此您需要明确指定它

可能类似于

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

首先,转到安装 python.exe 文件的文件夹,然后转到此目录:

\Lib\site-packages\PyInstaller\hooks

进入目录前必须先安装pyinstaller。 然后查看hooks文件夹,看看是否有一个名为:

的文件

hook-pyttsx3.py

文件很可能不存在。 因此,您必须在 hooks 文件夹中创建 hook-pyttsx3.py,并在必须写入的文件中创建:

#-----------------------------------------------------------------------------
# Copyright (c) 2013-2020, PyInstaller Development Team.
#
# Distributed under the terms of the GNU General Public License (version 2
# or later) with exception for distributing the bootloader.
#
# The full license is in the file COPYING.txt, distributed with this software.
#
# SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
#-----------------------------------------------------------------------------


""" pyttsx3 imports drivers module based on specific platform. Fount at https://github.com/nateshmbhat/pyttsx3/issues/6 """


hiddenimports = [
    'pyttsx3.drivers',
    'pyttsx3.drivers.dummy',
    'pyttsx3.drivers.espeak',
    'pyttsx3.drivers.nsss',
    'pyttsx3.drivers.sapi5', ]

保存文件。 然后 运行 你的代码。 问题将得到解决(至少它对我有用。) 出现这个问题是因为pyinstaller没有正式更新到完全使用python 3.8,所以一些模块的hooks丢失了

@Lakshya 给出的答案是正确的,但仅限于 Windows 环境。

对于使用Anaconda的用户,路径会发生变化。为了新用户的简单起见,这里是更新后的路径。

在这里,我使用的是一个名为 voice 的虚拟环境,因此它与 python3.7 一起在 envs/voice/ 中。这些可能因您的本地路径而异。

/anaconda3/envs/voice/lib/python3.7/site-packages/PyInstaller/hooks

如果你不使用虚拟环境,它看起来像,

/anaconda3/lib/python3.8/site-packages/PyInstaller/hooks

然后只需添加文件 hook-pyttsx3.py

#-----------------------------------------------------------------------------
# Copyright (c) 2013-2020, PyInstaller Development Team.
#
# Distributed under the terms of the GNU General Public License (version 2
# or later) with exception for distributing the bootloader.
#
# The full license is in the file COPYING.txt, distributed with this software.
#
# SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
#-----------------------------------------------------------------------------


""" pyttsx3 imports drivers module based on specific platform. Fount at https://github.com/nateshmbhat/pyttsx3/issues/6 """


hiddenimports = [
    'pyttsx3.drivers',
    'pyttsx3.drivers.dummy',
    'pyttsx3.drivers.espeak',
    'pyttsx3.drivers.nsss',
    'pyttsx3.drivers.sapi5', ]