Mac下pyttsx3初始化报错:'super' object has no attribute 'init'
解决Mac Ventura 13.4 + Python3.11下pyttsx3初始化报错问题
问题场景
使用Homebrew安装的Python3.11,运行pyttsx3初始化代码时出现核心错误:AttributeError: 'super' object has no attribute 'init',同时伴随ObjCSuperWarning和KeyError: None等提示。
问题原因
pyttsx3自带的nsss.py驱动(适配Mac系统语音合成的模块),其代码中对Objective-C父类方法的调用方式,在Python3.11+最新版PyObjC环境下存在兼容性问题——原代码使用Python内置的super()调用父类init方法,而PyObjC要求必须通过objc.super来实现Objective-C子类的父方法调用。
解决方案
直接修改pyttsx3的nsss.py驱动文件:
定位文件路径:
终端执行命令找到文件位置:find /usr/local/lib/python3.11 -name "nsss.py"通常路径为
/usr/local/lib/python3.11/site-packages/pyttsx3/drivers/nsss.py替换文件内容:
将原文件内容全部替换为以下修正后的代码:import objc from Foundation import * from AppKit import * import pyttsx3.driver NSSpeechSynthesizer = objc.lookUpClass('NSSpeechSynthesizer') NSVoice = objc.lookUpClass('NSVoice') class NSSpeechDriver(NSObject): @objc.python_method def initWithProxy(self, proxy): self = objc.super(NSSpeechDriver, self).init() if self is None: return None self._proxy = proxy self._synthesizer = NSSpeechSynthesizer.alloc().init() self._synthesizer.setDelegate_(self) return self def speechSynthesizer_didFinishSpeaking_(self, synth, finished): self._proxy.endLoop() @objc.python_method def buildDriver(proxy): return NSSpeechDriver.alloc().initWithProxy(proxy)重新运行代码,即可正常初始化pyttsx3并进行语音合成。
备选方案
如果不想修改源码,可尝试:
- 降级Python到3.10版本(pyttsx3对Python3.10的兼容性更稳定)
- 使用替代语音库,如
gTTS(基于Google Text-to-Speech,需网络支持)
内容的提问来源于stack exchange,提问作者Ansh Tyagi
相关产品推荐
相关产品推荐

