AttributeError: ‘super’对象没有属性’init’

huangapple go评论140阅读模式
英文:

AttributeError: 'super' object has no attribute 'init'

问题

错误:

/usr/local/lib/python3.11/site-packages/pyttsx3/drivers/nsss.py:12: ObjCSuperWarning: Objective-C subclass uses super(), but super is not objc.super
  class NSSpeechDriver(NSObject):
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/pyttsx3/__init__.py", line 20, in init
    eng = _activeEngines[driverName]
          ~~~~~~~~~~~~~~^^^^^^^^^^^^
  File "/usr/local/Cellar/python@3.11/3.11.3/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/anshtyagi/Documents/personal assistant/main.py", line 5, in <module>
    engine = pyttsx3.init()
             ^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pyttsx3/__init__.py", line 22, in init
    eng = Engine(driverName, debug)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pyttsx3/engine.py", line 30, in __init__
    self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pyttsx3/driver.py", line 52, in __init__
    self._driver = self._module.buildDriver(weakref.proxy(self))
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pyttsx3/drivers/nsss.py", line 9, in buildDriver
    return NSSpeechDriver.alloc().initWithProxy(proxy)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pyttsx3/drivers/nsss.py", line 15, in initWithProxy
    self = super(NSSpeechDriver, self).init()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'super' object has no attribute 'init'
sys:1: UninitializedDeallocWarning: leaking an uninitialized object of type NSSpeechDriver

我不知道问题出在哪里。还有一件事:由于某些问题,我不得不卸载了 Mac 上的旧 Python 版本,并使用 Homebrew 安装了新版本。

Mac OS ventura 13.4

Python 3.11

英文:

I was making a personal assistant. I got an error in starting code:

import pyttsx3
engine = pyttsx3.init()
engine.say('How are you today?')
engine.runAndWait()

Error:

/usr/local/lib/python3.11/site-packages/pyttsx3/drivers/nsss.py:12: ObjCSuperWarning: Objective-C subclass uses super(), but super is not objc.super
  class NSSpeechDriver(NSObject):
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/pyttsx3/__init__.py", line 20, in init
    eng = _activeEngines[driverName]
          ~~~~~~~~~~~~~~^^^^^^^^^^^^
  File "/usr/local/Cellar/python@3.11/3.11.3/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/anshtyagi/Documents/personal assistant/main.py", line 5, in <module>
    engine = pyttsx3.init()
             ^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pyttsx3/__init__.py", line 22, in init
    eng = Engine(driverName, debug)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pyttsx3/engine.py", line 30, in __init__
    self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pyttsx3/driver.py", line 52, in __init__
    self._driver = self._module.buildDriver(weakref.proxy(self))
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pyttsx3/drivers/nsss.py", line 9, in buildDriver
    return NSSpeechDriver.alloc().initWithProxy(proxy)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pyttsx3/drivers/nsss.py", line 15, in initWithProxy
    self = super(NSSpeechDriver, self).init()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'super' object has no attribute 'init'
sys:1: UninitializedDeallocWarning: leaking an uninitialized object of type NSSpeechDriver

I don't know what is the problem. One more thing: due to some issue I had to uninstall old python version on Mac and installed new one using Homebrew.

Mac OS ventura 13.4

Python 3.11

答案1

得分: 24

这部分的翻译如下:

在底层,这个模块 pyttsx3 使用 PyObjC 作为 Python 和 Objective-C 之间的桥梁。

步骤 1:检查是否已安装 pyobjc(运行 pip show pyobjc),如果没有,请运行 pip install pyobjc 安装。

步骤 2:打开这个文件 /usr/local/lib/python3.11/site-packages/pyttsx3/drivers/nsss.py,并进行以下更改:

#self = super(NSSpeechDriver, self).init() 注释掉这一行,并添加以下内容
self = objc.super(NSSpeechDriver, self).init()

注意from Foundation import * 导入了 NSObject 和来自 Foundation 的 objc,已经被使用。

做出这些更改后,你的下面的程序将正常运行:

import pyttsx3
engine = pyttsx3.init()
engine.say('你今天好吗?')
engine.runAndWait()
英文:

this turns out to be a little tricky. and this is a workaround! hope works for you.

Under the hood, this module pyttsx3 uses PyObjC as a bridge between Python and Objective-C.<br>

Step 1: Check that pyobjc is installed(pip show pyobjc), if not install as pip install pyobjc. <br>
Step 2: open this file /usr/local/lib/python3.11/site-packages/pyttsx3/drivers/nsss.py and change the following:

#self = super(NSSpeechDriver, self).init() comment this line , and add the following
self = objc.super(NSSpeechDriver, self).init()

AttributeError: ‘super’对象没有属性’init’

Note: from Foundation import * imports NSObject and objc from foundation, which has been consumed.

after the change, your following program would run okay.

import pyttsx3
engine = pyttsx3.init()
engine.say(&#39;How are you today?&#39;)
engine.runAndWait()

答案2

得分: 0

支持的Python版本似乎是3.5到3.7,如文档中所述。我可以确认版本3.6.15在我的Mac上可以正常工作。(但是,版本3.7.12不适用。)

英文:

It seems that the supported Python versions are 3.5 to 3.7, as stated in the documentation. I can confirm that version 3.6.15 works fine with my Mac. (However, version 3.7.12 doesn't work.)

huangapple
  • 本文由 发表于 2023年6月9日 01:58:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76434535.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定