pyttsx3: pyttsx3 fails to execute when compiled

Error when running Executable created with PyInstaller 3.3 on Windows 10 64-bit and Python v3.6. [pyttsx3_error.txt](https://github.com/nateshmbhat/pyttsx3/files/1455369/pyttsx3_error.txt) Example Script:

# import the speech to text module
import pyttsx3


# Initialize the Speech Engine
engine = pyttsx3.init()
# Set the words per minute rate of the Speech engine
engine.setProperty('rate', 105)
# Tell the engine what you want it to say.
engine.say('Sally sells seashells by the seashore.')
engine.say('The quick brown fox jumped over the lazy dog.')
# Tell the engine to start saying what you wanted it to and stop when it reaches the end of the queued sayings.
engine.runAndWait()

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 16 (1 by maintainers)

Most upvoted comments

I am using python 3.8

`import pyttsx3

engian = pyttsx3.init() engian.say(“hi”) engian.runAndWait()`

I am getting error like this

`Traceback (most recent call last): File “C:\python3.8\lib\site-packages\pyttsx3__init__.py”, line 20, in init eng = _activeEngines[driverName] File “C:\python3.8\lib\weakref.py”, line 131, in getitem o = self.datakey KeyError: None

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File “c:/Users/pande/Desktop/New folder/jarvis.py”, line 4, in engian = pyttsx3.init() File “C:\python3.8\lib\site-packages\pyttsx3__init__.py”, line 22, in init eng = Engine(driverName, debug) File “C:\python3.8\lib\site-packages\pyttsx3\engine.py”, line 30, in init self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug) File “C:\python3.8\lib\site-packages\pyttsx3\driver.py”, line 50, in init self.module = importlib.import_module(name) File "C:\python3.8\lib\importlib__init_.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File “”, line 1014, in _gcd_import File “”, line 991, in _find_and_load File “”, line 975, in _find_and_load_unlocked File “”, line 671, in _load_unlocked File “”, line 783, in exec_module File “”, line 219, in _call_with_frames_removed File “C:\python3.8\lib\site-packages\pyttsx3\drivers\sapi5.py”, line 10, in import pythoncom ModuleNotFoundError: No module named ‘pythoncom’` I am in win 10

try installing “pip install pyttsx3==2.71”

I am using python 3.8

`import pyttsx3

engian = pyttsx3.init() engian.say(“hi”) engian.runAndWait()`

I am getting error like this

`Traceback (most recent call last): File “C:\python3.8\lib\site-packages\pyttsx3_init_.py”, line 20, in init
eng = _activeEngines[driverName] File “C:\python3.8\lib\weakref.py”, line 131, in getitem o = self.datakey KeyError: None

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File “c:/Users/pande/Desktop/New folder/jarvis.py”, line 4, in <module> engian = pyttsx3.init() File “C:\python3.8\lib\site-packages\pyttsx3_init_.py”, line 22, in init eng = Engine(driverName, debug) File “C:\python3.8\lib\site-packages\pyttsx3\engine.py”, line 30, in init self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug) File “C:\python3.8\lib\site-packages\pyttsx3\driver.py”, line 50, in init self.module = importlib.import_module(name) File "C:\python3.8\lib\importlib_init.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File “<frozen importlib._bootstrap>”, line 1014, in _gcd_import File “<frozen importlib._bootstrap>”, line 991, in _find_and_load File “<frozen importlib._bootstrap>”, line 975, in _find_and_load_unlocked File “<frozen importlib._bootstrap>”, line 671, in _load_unlocked File “<frozen importlib._bootstrap_external>”, line 783, in exec_module File “<frozen importlib._bootstrap>”, line 219, in _call_with_frames_removed File “C:\python3.8\lib\site-packages\pyttsx3\drivers\sapi5.py”, line 10, in <module> import pythoncom ModuleNotFoundError: No module named ‘pythoncom’` I am in win 10

Example of what fixes the No module named pyttsx3.drivers error:

These are the key lines: from PyInstaller.utils.hooks import collect_submodules my_hidden_imports = collect_submodules(‘pyttsx3’) hiddenimports=my_hidden_imports,

This is a .spec file I created that collects all of the pyttsx3 submodules:

# -*- mode: python -*-
from PyInstaller.utils.hooks import collect_submodules
block_cipher = None

my_hidden_imports = collect_submodules('pyttsx3')

a = Analysis(['pyttsx3_example.py'],
             pathex=['C:\\Program Files (x86)\\Windows Kits\\10\\Redist\\ucrt\\DLLs\\x64'],
             binaries=[],
             datas=[],
             hiddenimports=my_hidden_imports,
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
		  a.zipfiles,
		  a.datas,
          name='pyttsx3_example',
          debug=False,
          strip=False,
          upx=True,
          console=True )

Example hook file for pyttsx3 that avoids needing to add any of that into the spec file:

#-----------------------------------------------------------------------------
# Copyright (c) 2013-2017, PyInstaller Development Team.
#
# Distributed under the terms of the GNU General Public License with exception
# for distributing bootloader.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------


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

Current Error when trying to run pyttsx3 from a Windows 10 64-bit Pyinstaller compiled executible.

Traceback (most recent call last): File “site-packages\pyttsx3_init_.py”, line 44, in init File “c:\users\my_user\appdata\local\programs\python\python36\lib\weakref.py”, line 137, in getitem o = self.datakey KeyError: ‘sapi5’

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File “pyttsx3_example.py”, line 9, in <module> engine = pyttsx3.init(‘sapi5’) 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 77, in init File “site-packages\pyttsx3\drivers\sapi5.py”, line 22, in buildDriver File “site-packages\pyttsx3\drivers\sapi5.py”, line 41, in init File “site-packages\pyttsx3\drivers\sapi5.py”, line 83, in setProperty File “site-packages\win32com\client\dynamic.py”, line 549, in setattr pywintypes.com_error: (-2147352573, ‘Member not found.’, None, None) [2244] Failed to execute script pyttsx3_example

Traceback (most recent call last): File “site-packages\pyttsx3_init_.py”, line 44, in init File “c:\users\MyUser\appdata\local\programs\python\python36\lib\weakref.py”, line 137, in getitem o = self.datakey KeyError: None

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File “pyttsx3_example.py”, line 9, 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 126, in import_module File “<frozen importlib._bootstrap>”, line 994, in _gcd_import File “<frozen importlib._bootstrap>”, line 971, in _find_and_load File “<frozen importlib._bootstrap>”, line 941, in _find_and_load_unlocked File “<frozen importlib._bootstrap>”, line 219, in _call_with_frames_removed File “<frozen importlib._bootstrap>”, line 994, in _gcd_import File “<frozen importlib._bootstrap>”, line 971, in _find_and_load File “<frozen importlib._bootstrap>”, line 953, in _find_and_load_unlocked ModuleNotFoundError: No module named ‘pyttsx3.drivers’ [10288] Failed to execute script pyttsx3_example

Check this #1