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

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

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

问题

错误:

  1. /usr/local/lib/python3.11/site-packages/pyttsx3/drivers/nsss.py:12: ObjCSuperWarning: Objective-C subclass uses super(), but super is not objc.super
  2. class NSSpeechDriver(NSObject):
  3. Traceback (most recent call last):
  4. File "/usr/local/lib/python3.11/site-packages/pyttsx3/__init__.py", line 20, in init
  5. eng = _activeEngines[driverName]
  6. ~~~~~~~~~~~~~~^^^^^^^^^^^^
  7. 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__
  8. o = self.data[key]()
  9. ~~~~~~~~~^^^^^
  10. KeyError: None
  11. During handling of the above exception, another exception occurred:
  12. Traceback (most recent call last):
  13. File "/Users/anshtyagi/Documents/personal assistant/main.py", line 5, in <module>
  14. engine = pyttsx3.init()
  15. ^^^^^^^^^^^^^^
  16. File "/usr/local/lib/python3.11/site-packages/pyttsx3/__init__.py", line 22, in init
  17. eng = Engine(driverName, debug)
  18. ^^^^^^^^^^^^^^^^^^^^^^^^^
  19. File "/usr/local/lib/python3.11/site-packages/pyttsx3/engine.py", line 30, in __init__
  20. self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug)
  21. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  22. File "/usr/local/lib/python3.11/site-packages/pyttsx3/driver.py", line 52, in __init__
  23. self._driver = self._module.buildDriver(weakref.proxy(self))
  24. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  25. File "/usr/local/lib/python3.11/site-packages/pyttsx3/drivers/nsss.py", line 9, in buildDriver
  26. return NSSpeechDriver.alloc().initWithProxy(proxy)
  27. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  28. File "/usr/local/lib/python3.11/site-packages/pyttsx3/drivers/nsss.py", line 15, in initWithProxy
  29. self = super(NSSpeechDriver, self).init()
  30. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  31. AttributeError: 'super' object has no attribute 'init'
  32. 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:

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

Error:

  1. /usr/local/lib/python3.11/site-packages/pyttsx3/drivers/nsss.py:12: ObjCSuperWarning: Objective-C subclass uses super(), but super is not objc.super
  2. class NSSpeechDriver(NSObject):
  3. Traceback (most recent call last):
  4. File "/usr/local/lib/python3.11/site-packages/pyttsx3/__init__.py", line 20, in init
  5. eng = _activeEngines[driverName]
  6. ~~~~~~~~~~~~~~^^^^^^^^^^^^
  7. 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__
  8. o = self.data[key]()
  9. ~~~~~~~~~^^^^^
  10. KeyError: None
  11. During handling of the above exception, another exception occurred:
  12. Traceback (most recent call last):
  13. File "/Users/anshtyagi/Documents/personal assistant/main.py", line 5, in <module>
  14. engine = pyttsx3.init()
  15. ^^^^^^^^^^^^^^
  16. File "/usr/local/lib/python3.11/site-packages/pyttsx3/__init__.py", line 22, in init
  17. eng = Engine(driverName, debug)
  18. ^^^^^^^^^^^^^^^^^^^^^^^^^
  19. File "/usr/local/lib/python3.11/site-packages/pyttsx3/engine.py", line 30, in __init__
  20. self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug)
  21. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  22. File "/usr/local/lib/python3.11/site-packages/pyttsx3/driver.py", line 52, in __init__
  23. self._driver = self._module.buildDriver(weakref.proxy(self))
  24. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  25. File "/usr/local/lib/python3.11/site-packages/pyttsx3/drivers/nsss.py", line 9, in buildDriver
  26. return NSSpeechDriver.alloc().initWithProxy(proxy)
  27. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  28. File "/usr/local/lib/python3.11/site-packages/pyttsx3/drivers/nsss.py", line 15, in initWithProxy
  29. self = super(NSSpeechDriver, self).init()
  30. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  31. AttributeError: 'super' object has no attribute 'init'
  32. 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,并进行以下更改:

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

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

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

  1. import pyttsx3
  2. engine = pyttsx3.init()
  3. engine.say('你今天好吗?')
  4. 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:

  1. #self = super(NSSpeechDriver, self).init() comment this line , and add the following
  2. 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.

  1. import pyttsx3
  2. engine = pyttsx3.init()
  3. engine.say(&#39;How are you today?&#39;)
  4. 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:

确定