3

I get this error from the exe generated by PyInstaller when using Pyttsx3. The code works fine in python. I've tried using other versions of PyInstaller and Pyttsx but it doesn't make a difference. I've also tried Py2exe which is also not working with Pyttsx3, does anyone know what's causing this?

The code

import pyttsx3 
engine = pyttsx3.init()

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

The error after running the exe generated

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'
Scott
  • 31
  • 1
  • 3
  • Can you post the python code in your question? – AMACB Jun 25 '18 at 13:28
  • @AMACD updated :) – Scott Jun 25 '18 at 13:35
  • chances are this is because it is not including the files it needs to run this module. I'm assuming pyttsx3 has C/C++ extensions, and pyinstaller isn't finding them all. I would compare your pyttsx3 directory with the one pyinstaller generates, to see what files you are likely missing – Michael Jun 25 '18 at 21:39

4 Answers4

4

Try this:

import pyttsx3
from pyttsx3.drivers import sapi5

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

Explanation:

You actually need to import an extra module from pyttsx3.

Itsjul1an
  • 301
  • 1
  • 2
  • 12
2

Goto location: C:\Users\username\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\PyInstaller\hooks

Create a newfile "hook-pyttsx3.py"

inside file, copy the code below..

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

Now your program will run without getting error.

Click here Github issue created

1

Try this:

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

Hidden import argument for pyinstaller import 3 rd party packages into build. By adding above lines to pyinstaller will create a spec file with hidden-import =[‘pyttsx3.drivers’,’pyttsx3.drivers.dummy’,....] which will rectify the error “no module named pyttsx.driver’ but eventually u will end up other error also which i am unable to solve.

Moshe Slavin
  • 5,127
  • 5
  • 23
  • 38
  • 3
    Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation would greatly improve its long-term value](//meta.stackexchange.com/q/114762/206345) by showing _why_ this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you've made. – Suraj Rao Mar 02 '19 at 09:58
0

I've just fixed pyttsx3 compatibility in #101. In a couple of weeks you will be able to:

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

but until then you can use the Github version:

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

Add the --clean option the first time you run PyInstaller after using pip (unless you're using auto-py-to-exe which blocks PyInstaller's caching). It should then work on all platforms without using any --hiddenimport-ing.

pullmyteeth
  • 462
  • 5
  • 12
  • I'm not super familiar with the pyinstaller-hooks-contrib package ... will this work with older versions of pyinstaller? I need to use pyinstaller==3.4 due to using fbs, and running into the same problem with pyttsx3 not packaging correctly – Simon Jun 28 '21 at 08:26
  • 1
    No you need at least PyInstaller 4.0 to use the hooks repo. If you can't upgrade then use [Lakshman Mallidi's answer](https://stackoverflow.com/a/54957185/7390688). That's the only answer here that's actually *correct*. – pullmyteeth Jun 29 '21 at 09:13