Search code examples
pythonmacospyttsx3nss

NameError/KeyErrors while running Pyttsx3 on Mac


I just wrote this simple code to test pyttsx3.

import pyttsx3 as pt

engine = pt.init()

engine.say("hi this is python")

engine.runAndWait()

I have worked with pyttsx3 in windows and can figure out why is this is giving me this kind of error:

Traceback (most recent call last):
  File "/opt/homebrew/lib/python3.11/site-packages/pyttsx3/__init__.py", line 20, in init
    eng = _activeEngines[driverName]
          ~~~~~~~~~~~~~~^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.6_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/weakref.py", line 136, in __getitem__
    o = self.data[key]()
        ~~~~~~~~~^^^^^
KeyError: None

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/<Path to file>", line 3, in <module>
    engine = pt.init()
             ^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/pyttsx3/__init__.py", line 22, in init
    eng = Engine(driverName, debug)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/pyttsx3/engine.py", line 30, in __init__
    self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/pyttsx3/driver.py", line 50, in __init__
    self._module = importlib.import_module(name)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.6_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/opt/homebrew/lib/python3.11/site-packages/pyttsx3/drivers/nsss.py", line 12, in <module>
    class NSSpeechDriver(NSObject):
  File "/opt/homebrew/lib/python3.11/site-packages/pyttsx3/drivers/nsss.py", line 13, in NSSpeechDriver
    @objc.python_method
     ^^^^
NameError: name 'objc' is not defined. Did you mean: 'object'?

What I searched an got was that there was problem with pyttsx3 on Ventura update. It there a fix around this?

I tried changing some imports and variables/keys by cloning the repo and trying to run it as local module but no luck!


Solution

  • Ok, what I found was pyttsx3 is kind of not being well maintained.

    The point is to use py3-tts instead of pyttsx3.

    just pip uninstall pyttsx3 and pip install py3-tts

    More details - https://pypi.org/project/py3-tts/