pyttsx3 |在 MacO 上初始化时出现问题

问题描述 投票:0回答:1

我有一个关于 Python 的“文本到语音”模块 pyttsx3 的问题。

我的目标:

  • 运行 pyttsx3 说出一串文本。

我的立场:

  • 安装了pyttsx3
  • 在 VS Code 中写入以下几行:
import pyttsx3
  engine = pyttsx3.init("nsss")
  engine.say("Hello World")
  engine.runAndWait()

我被困的地方:

  • 代码将无法正确初始化。

请参阅下面的错误消息:

 Traceback (most recent call last):
      File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-                         packages/pyttsx3/__init__.py", line 20, in init
        eng = _activeEngines[driverName]
          ~~~~~~~~~~~~~~^^^^^^^^^^^^
      File     "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/weakref.py", line     136, in __getitem__
        o = self.data[key]()
        ~~~~~~~~~^^^^^
    KeyError: 'nsss'

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "/Users/drewpeterson/Documents/Programming/Python/CS50/speech.py", line 6, in <module>
        engine = pyttsx3.init("nsss")
                 ^^^^^^^^^^^^^^^^^^^^
      File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-    packages/pyttsx3/__init__.py", line 22, in init
        eng = Engine(driverName, debug)
              ^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-        packages/pyttsx3/engine.py", line 30, in __init__
        self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/pyttsx3/driver.py", line 50, in __init__
        self._module = importlib.import_module(name)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File         "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/importlib/__init__.py", line 90, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "<frozen importlib._bootstrap>", line 1381, in _gcd_import
      File "<frozen importlib._bootstrap>", line 1354, in _find_and_load
      File "<frozen importlib._bootstrap>", line 1325, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 929, in _load_unlocked
      File "<frozen importlib._bootstrap_external>", line 994, in exec_module
      File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
      File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-    packages/pyttsx3/drivers/nsss.py", line 2, in <module>
        from Foundation import *
      File "<frozen importlib._bootstrap>", line 1403, in _handle_fromlist
      File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/objc/_lazyimport.py", line 160, in __getattr__
        value = self.__calc_all()
            ^^^^^^^^^^^^^^^^^
      File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/objc/_lazyimport.py", line 328, in __calc_all
        all_names.update(p.__all__)
                     ^^^^^^^^^
      File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/objc/_lazyimport.py", line 160, in __getattr__
        value = self.__calc_all()
            ^^^^^^^^^^^^^^^^^
      File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/objc/_lazyimport.py", line 257, in __calc_all
        objc.loadBundleVariables(self.__bundle, dct, varmap)
      File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/objc/_pythonify.py", line 52, in numberWrapper
        if isinstance(obj, NSDecimalNumber):
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    objc.internal_error: PyObjC: internal error in _type_lookup at Modules/objc/objc-        object.m:380: assertion failed: dict && PyDict_Check(dict)
python macos text-to-speech pyobjc pyttsx3
1个回答
0
投票

您无法在 Mac 上使用

pyttsx3
初始化
nsss
。我也遇到过同样的问题。

运行

pip install py3-tts
。您不再需要
 pyttsx3.init()
的司机了。

© www.soinside.com 2019 - 2024. All rights reserved.