AttributeError: 'super' object has no attribute 'init'
Asked Answered
N

3

7

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/[email protected]/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

Neptune answered 8/6, 2023 at 17:58 Comment(5)
sorry, can't recreate , works okay for me(on mac Catalina, and pyttsx3 version 2.90), I think some dependency is missing for pyttsx3, try to reinstall pyttsx3 and look for warnings.Copulative
I reinstalled it but no fix also their are no warnings while installing it.Neptune
@Copulative check aboveNeptune
created the virtual env and was able to recreate the problem. problem is with driver (nsss) [github.com/nateshmbhat/pyttsx3/blob/master/pyttsx3/drivers/…Copulative
@Copulative same issue in venvNeptune
C
28

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.

Step 1: Check that pyobjc is installed(pip show pyobjc), if not install as pip install pyobjc.
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()

enter image description here

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('How are you today?')
engine.runAndWait()
Copulative answered 10/6, 2023 at 1:52 Comment(0)
C
0

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.)

Convalescence answered 31/7, 2023 at 2:32 Comment(0)
O
0

You may also need to add to your nsss.py file

import objc
Oui answered 2/2 at 3:23 Comment(1)
Welcome to Stack Overflow. Please take the tour and read How to Answer, and note well that this is not a discussion forum. If you think that an existing answer is missing a step, consider proposing an edit to the answer. At 50 reputation you can leave comments with feedback on answers by others. But at any rate, the answer section is only for attempts to answer the question, as written.Chloroplast

© 2022 - 2024 — McMap. All rights reserved.