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

时间:2019-09-27 10:53:03

标签: python pyinstaller pyttsx

我使用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

3 个答案:

答案 0 :(得分:5)

首先,转到安装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,因此缺少某些模块的钩子

答案 1 :(得分:0)

看看文档的When things went wrong部分 特别是Listing hidden imports

看起来pyinstaller无法“知道”它需要添加此特定模块,因此您需要明确指定它

可能类似于

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

答案 2 :(得分:0)

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

对于使用Anaconda的用户,路径会更改。为了简化新用户,这是更新的路径。

在这里,我正在使用一个名为voice的虚拟环境,因此它在envs/voice/python3.7中。这些可能会根据您的本地路径而有所不同。

/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', ]