4

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

3 回答 3

7

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

\Lib\site-packages\PyInstaller\hooks

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

钩-pyttsx3.py

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

#-----------------------------------------------------------------------------
# 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,所以缺少一些模块的钩子

于 2020-07-10T08:03:56.967 回答
2

查看文档的When things go wrong部分,特别是,Listing hidden imports

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

可能是这样的

$ pyinstaller --hidden-import=pyttsx3.drivers song_dl.py
于 2019-09-27T11:03:19.420 回答
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', ]
于 2020-11-10T07:17:39.667 回答