Python 3.9.4 64位运行pyttsx3报错,寻求技术解决方向
语音识别与合成代码运行报错及解决方法
尝试运行语音识别与合成代码,已重装pyttsx、安装pypiwin32并指定安装pyttsx==2.71,仍无法正常运行。
运行代码
# Imports import speech_recognition as sr import pyttsx3 # Voice Recognition listener = sr.Recognizer() speaker = pyttsx3.init() with sr.Microphone() as input_device: print("I am ready") voice_content = listener.listen(input_device) voice_text = listener.recognize_google(voice_content) print(voice_text) speaker.say("Hello") speaker.runAndWait()
报错信息
Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pyttsx3/__init__.py", line 20, in init eng = _activeEngines[driverName] File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/weakref.py", line 134, in __getitem__ o = self.data[key]() KeyError: None During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Users/jackmaling/Desktop/Personal/Personal CS/Kitchen Voice Assistant (Soux)/voice_bot_testing.py", line 11, in <module> speaker = pyttsx3.init() File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pyttsx3/__init__.py", line 22, in init eng = Engine(driverName, debug) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/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.9/lib/python3.9/site-packages/pyttsx3/driver.py", line 50, in __init__ self._module = importlib.import_module(name) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1030, in _gcd_import File "<frozen importlib._bootstrap>", line 1007, in _find_and_load File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 680, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 790, in exec_module File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pyttsx3/drivers/nsss.py", line 12, in <module> class NSSpeechDriver(NSObject): File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pyttsx3/drivers/nsss.py", line 13, in NSSpeechDriver @objc.python_method NameError: name 'objc' is not defined
解决方向
- 安装pyobjc依赖:报错显示缺少
objc模块,这是macOS系统下pyttsx3依赖的核心模块,执行命令:pip install pyobjc - 手动指定驱动:若安装后仍报错,修改初始化代码为
speaker = pyttsx3.init(driverName='nsss'),确保系统使用NSSpeechSynthesizer驱动 - 检查Python版本兼容性:pyttsx3对Python3.9的支持可能存在兼容问题,可尝试切换到Python3.8或3.10版本测试
- 重新安装最新版pyttsx3:卸载现有版本后安装最新版,执行命令:
pip uninstall pyttsx3 && pip install pyttsx3,避免旧版本2.71带来的兼容问题
内容的提问来源于stack exchange,提问作者Jack Maling
相关产品推荐
相关产品推荐

