Pyttsx3无法与PyInstaller一起使用

时间:2018-06-25 13:14:56

标签: python-3.x pyinstaller py2exe pyttsx

当我使用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 个答案:

答案 0 :(得分:1)

尝试一下:

import pyttsx3
from pyttsx3.drivers import sapi5

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

说明:

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

答案 1 :(得分:0)

尝试一下:

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方软件包导入构建。通过将以上行添加到pyinstaller中,将创建一个带有hidden-import = ['pyttsx3.drivers','pyttsx3.drivers.dummy',....]的规范文件,这将纠正错误“没有名为pyttsx.driver的模块”,但最终,您将遇到其他我也无法解决的错误。

答案 2 :(得分:0)

转到位置: C:\ Users \用户名\ 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 issue created

答案 3 :(得分:0)

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

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

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

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

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