【问题标题】:Pyttsx3 not working with PyInstallerPyttsx3 不适用于 PyInstaller
【发布时间】:2018-06-25 13:14:56
【问题描述】:

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

【问题讨论】:

  • 你可以在你的问题中发布python代码吗?
  • @AMACD 已更新 :)
  • 可能是因为它不包括运行此模块所需的文件。我假设 pyttsx3 有 C/C++ 扩展,而 pyinstaller 并没有找到它们。我会将您的 pyttsx3 目录与 pyinstaller 生成的目录进行比较,以查看您可能缺少哪些文件

标签: python-3.x pyinstaller py2exe pyttsx


【解决方案1】:

试试这个:

import pyttsx3
from pyttsx3.drivers import sapi5

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

解释:

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

【讨论】:

    【解决方案2】:

    转到位置: 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】:

      试试这个:

      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 的模块”但是最终你会遇到其他我无法解决的错误。

      【讨论】:

      • 感谢您提供此代码 sn-p,它可能会提供一些有限的即时帮助。 proper explanation would greatly improve its long-term value 通过展示为什么这是一个很好的解决问题的方法,并将使其对未来有其他类似问题的读者更有用。请edit您的回答添加一些解释,包括您所做的假设。
      【解决方案4】:

      我刚刚修复了#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。

      【讨论】:

      • 我对 pyinstaller-hooks-contrib 包不是很熟悉...这适用于旧版本的 pyinstaller 吗?由于使用 fbs,我需要使用 pyinstaller==3.4,并且遇到 pyttsx3 未正确打包的相同问题
      • 不,您至少需要 PyInstaller 4.0 才能使用 hooks 存储库。如果您无法升级,请使用Lakshman Mallidi's answer。这是这里唯一真正正确的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-04
      • 1970-01-01
      • 2022-06-20
      • 2021-09-20
      • 1970-01-01
      相关资源
      最近更新 更多