Search code examples
python-3.xmacospyttsx3

Pyttsx3 doesn't work on Python 3.11 script, Mac M1


I get a weird error when I try to execute the initialization of Pyttssx3 module (successfully installed), in a Python 3.11 script. I just executed:

import pyttsx3
engine=pyttsx3.init()

And I get, from the related nsss.py library:

class NSSpeechDriver(NSObject):
    @objc.python_method
    NameError: name 'objc' is not defined. Did you mean: 'object'?

Stacktrace:

Traceback (most recent call last):
  File ".venv/lib/python3.10/site-packages/pyttsx3/__init__.py", line 20, in init
    eng = _activeEngines[driverName]
  File "~/.pyenv/versions/3.10.7/lib/python3.10/weakref.py", line 137, in __getitem__
    o = self.data[key]()
KeyError: 'nsss'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "~/.pyenv/versions/3.10.7/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "~/.pyenv/versions/3.10.7/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "~/dev/thea/thea/tts.py", line 13, in <module>
    main()
  File "~/dev/thea/thea/tts.py", line 7, in main
    engine = init(driverName="nsss")
  File ".venv/lib/python3.10/site-packages/pyttsx3/__init__.py", line 22, in init
    eng = Engine(driverName, debug)
  File ".venv/lib/python3.10/site-packages/pyttsx3/engine.py", line 30, in __init__
    self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug)
  File ".venv/lib/python3.10/site-packages/pyttsx3/driver.py", line 50, in __init__
    self._module = importlib.import_module(name)
  File "~/.pyenv/versions/3.10.7/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File ".venv/lib/python3.10/site-packages/pyttsx3/drivers/nsss.py", line 12, in <module>
    class NSSpeechDriver(NSObject):
  File ".venv/lib/python3.10/site-packages/pyttsx3/drivers/nsss.py", line 13, in NSSpeechDriver
    @objc.python_method
NameError: name 'objc' is not defined. Did you mean: 'object'?

Solution

  • Installing this package solved the problem for me.

    pip install py3-tts