3

使用 Pyttsx3 时,我从 PyInstaller 生成的 exe 中收到此错误。该代码在python中运行良好。我尝试过使用其他版本的 PyInstaller 和 Pyttsx,但这并没有什么不同。我也试过 Py2exe,它也不能与 Pyttsx3 一起工作,有人知道是什么原因造成的吗?

编码

import pyttsx3 
engine = pyttsx3.init()

engine.say('Test') 
engine.runAndWait()

运行exe后产生的错误

Traceback (most recent call last):
  File "site-packages\pyttsx3\__init__.py", line 44, in init
  File "c:\python34\lib\weakref.py", line 125, in __getitem__
    o = self.data[key]()
KeyError: None

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "Test.py", line 85, in <module>
  File "site-packages\pyttsx3\__init__.py", line 46, in init
  File "site-packages\pyttsx3\engine.py", line 52, in __init__
  File "site-packages\pyttsx3\driver.py", line 75, in __init__
  File "importlib\__init__.py", line 109, in import_module
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2212, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2224, in _find_and_load_unlocked
ImportError: No module named 'pyttsx3.drivers'
4

4 回答 4

4

尝试这个:

import pyttsx3
from pyttsx3.drivers import sapi5

engine = pyttsx3.init()
engine.say('Test')
engine.runAndWait()

解释:

您实际上需要从 pyttsx3 导入一个额外的模块。

于 2020-08-09T17:43:09.953 回答
2

转到位置:C:\Users\username\AppData\Local\Programs\Python\Python38-32\Lib\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', ]

现在您的程序将运行而不会出错。

单击此处创建的 Github 问题

于 2020-08-16T21:11:29.033 回答
1

尝试这个:

pyinstaller --hidden-import=pyttsx3.drivers --hidden-import=pyttsx3.drivers.dummy --hidden-import=pyttsx3.drivers.espeak --hidden-import=pyttsx3.drivers.nsss --hidden-import=pyttsx3.drivers.sapi5

pyinstaller 的隐藏导入参数将 3 rd 方包导入构建。通过将上述行添加到 pyinstaller 将创建一个带有 hidden-import =['pyttsx3.drivers','pyttsx3.drivers.dummy',....] 的规范文件,这将纠正错误“没有名为 pyttsx.driver 的模块”但是最终你会遇到其他我无法解决的错误。

于 2019-03-02T09:46:19.443 回答
0

我刚刚在#101pyttsx3中修复了兼容性。几周后,您将能够:

pip install "pyinstaller-hooks-contrib>=2021.2"

但在那之前你可以使用 Github 版本:

pip install -U https://github.com/pyinstaller/pyinstaller-hooks-contrib/archive/refs/heads/master.zip

--clean在使用 pip 后第一次运行 PyInstaller 时添加该选项(除非您正在使用auto-py-to-exe阻止 PyInstaller 缓存的选项)。然后它应该可以在所有平台上工作,而无需使用任何--hiddenimport-ing。

于 2021-03-21T10:38:15.037 回答