nateshmbhat / pyttsx3

Offline Text To Speech synthesis for python
Mozilla Public License 2.0
1.99k stars 321 forks source link

Persistent NameError when specifying engine, likely related to 'pyobjc' package #301

Open mxdoerfler opened 6 months ago

mxdoerfler commented 6 months ago

This is my basic code which breaks on the second line 'engine = pyttsx3.init()'

import pyttsx3
engine = pyttsx3.init()
#engine = pyttsx3.init('dummy')
#engine = pyttsx3.init(driverName='nsss')
name = input("What's your name? ")
engine.say(f"hello, {name}")
engine.runAndWait()

I have tried variations both with 'dummy' and specifying the driver explicity and all give me the same error:

{--------------------------------------------------------------------------- KeyError Traceback (most recent call last) File ~/anaconda3/envs/testing1/lib/python3.12/site-packages/pyttsx3/init.py:20, in init(driverName, debug) 19 try: ---> 20 eng = _activeEngines[driverName] 21 except KeyError: File ~/anaconda3/envs/testing1/lib/python3.12/weakref.py:136, in WeakValueDictionary.getitem(self, key) 135 self._commit_removals() --> 136 o = self.data[key]() 137 if o is None: KeyError: 'nsss' During handling of the above exception, another exception occurred: NameError Traceback (most recent call last) Cell In[4], line 5 1 import pyttsx3 3 #engine = pyttsx3.init() 4 #engine = pyttsx3.init('dummy') ----> 5 engine = pyttsx3.init(driverName='nsss') 6 name = input("What's your name? ") 7 engine.say(f"hello, {name}") File ~/anaconda3/envs/testing1/lib/python3.12/site-packages/pyttsx3/init.py:22, in init(driverName, debug) 20 eng = _activeEngines[driverName] 21 except KeyError: ---> 22 eng = Engine(driverName, debug) 23 _activeEngines[driverName] = eng 24 return eng File ~/anaconda3/envs/testing1/lib/python3.12/site-packages/pyttsx3/engine.py:30, in Engine.init(self, driverName, debug) 20 def init(self, driverName=None, debug=False): 21 """ 22 Constructs a new TTS engine instance. 23 (...) 28 @type debug: bool 29 """ ---> 30 self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug) 31 # initialize other vars 32 self._connects = {} File ~/anaconda3/envs/testing1/lib/python3.12/site-packages/pyttsx3/driver.py:50, in DriverProxy.init(self, engine, driverName, debug) 48 # import driver module 49 name = 'pyttsx3.drivers.%s' % driverName ---> 50 self._module = importlib.import_module(name) 51 # build driver instance 52 self._driver = self._module.buildDriver(weakref.proxy(self)) File ~/anaconda3/envs/testing1/lib/python3.12/importlib/init.py:90, in import_module(name, package) 88 break 89 level += 1 ---> 90 return _bootstrap._gcd_import(name[level:], package, level) File :1381, in _gcd_import(name, package, level) File :1354, in _find_andload(name, import) File :1325, in _find_and_loadunlocked(name, import) File :929, in _load_unlocked(spec) File :994, in exec_module(self, module) File :488, in _call_with_frames_removed(f, *args, **kwds) File ~/anaconda3/envs/testing1/lib/python3.12/site-packages/pyttsx3/drivers/nsss.py:12 8 def buildDriver(proxy): 9 return NSSpeechDriver.alloc().initWithProxy(proxy) ---> 12 class NSSpeechDriver(NSObject): 13 @objc.python_method 14 def initWithProxy(self, proxy): 15 self = super(NSSpeechDriver, self).init() File ~/anaconda3/envs/testing1/lib/python3.12/site-packages/pyttsx3/drivers/nsss.py:13, in NSSpeechDriver() 12 class NSSpeechDriver(NSObject): ---> 13 @objc.python_method 14 def initWithProxy(self, proxy): 15 self = super(NSSpeechDriver, self).init() 16 if self: NameError: name 'objc' is not defined

I guess it has something to do with communication between the pyttsx3 and pyobjc packages.

Any ideas highly appreciated since this is beyond me now.

Akul2010 commented 6 months ago

@mxdoerfler Hi, I just released a new update that might have fixed it. Sadly, as I do not have a MacBook, I can't support OS X testing. I would appreciate feedback, and see if it works.

mxdoerfler commented 6 months ago

Downloaded rlvoice from pip just now but still got the same error. See below. Any other ideas? And is this latest version on pip or only github?

Also I am using python 3.12 is rlvoice compatible?

My code

import rlvoice

engine = rlvoice.init()

The error I got:


KeyError Traceback (most recent call last) File ~/anaconda3/envs/carpentry/lib/python3.12/site-packages/rlvoice/init.py:20, in init(driverName, debug) 19 try: ---> 20 eng = _activeEngines[driverName] 21 except KeyError: File ~/anaconda3/envs/carpentry/lib/python3.12/weakref.py:136, in WeakValueDictionary.getitem(self, key) 135 self._commit_removals() --> 136 o = self.data[key]() 137 if o is None: KeyError: None During handling of the above exception, another exception occurred: NameError Traceback (most recent call last) /Users/mdoerfler/Desktop/CS50/week6/t2s_test.py in line 1 ----> 4 engine = rlvoice.init() File ~/anaconda3/envs/carpentry/lib/python3.12/site-packages/rlvoice/init.py:22, in init(driverName, debug) 20 eng = _activeEngines[driverName] 21 except KeyError: ---> 22 eng = Engine(driverName, debug) 23 _activeEngines[driverName] = eng 24 return eng File ~/anaconda3/envs/carpentry/lib/python3.12/site-packages/rlvoice/engine.py:30, in Engine.init(self, driverName, debug) 20 def init(self, driverName=None, debug=False): 21 """ 22 Constructs a new TTS engine instance. 23 (...) 28 @type debug: bool 29 """ ---> 30 self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug) 31 # initialize other vars 32 self._connects = {} File ~/anaconda3/envs/carpentry/lib/python3.12/site-packages/rlvoice/driver.py:50, in DriverProxy.init(self, engine, driverName, debug) 48 # import driver module 49 name = 'rlvoice.drivers.%s' % driverName ---> 50 self._module = importlib.import_module(name) 51 # build driver instance 52 self._driver = self._module.buildDriver(weakref.proxy(self)) File ~/anaconda3/envs/carpentry/lib/python3.12/importlib/init.py:90, in import_module(name, package) 88 break 89 level += 1 ---> 90 return _bootstrap._gcd_import(name[level:], package, level) File :1381, in _gcd_import(name, package, level) File :1354, in _find_andload(name, import) File :1325, in _find_and_loadunlocked(name, import) File :929, in _load_unlocked(spec) File :994, in exec_module(self, module) File :488, in _call_with_frames_removed(f, *args, **kwds) File ~/anaconda3/envs/carpentry/lib/python3.12/site-packages/rlvoice/drivers/nsss.py:11 7 def buildDriver(proxy): 8 return NSSpeechDriver.alloc().initWithProxy(proxy) ---> 11 class NSSpeechDriver(NSObject): 12 @objc.python_method 13 def initWithProxy(self, proxy): 14 self = super(NSSpeechDriver, self).init() File ~/anaconda3/envs/carpentry/lib/python3.12/site-packages/rlvoice/drivers/nsss.py:12, in NSSpeechDriver() 11 class NSSpeechDriver(NSObject): ---> 12 @objc.python_method 13 def initWithProxy(self, proxy): 14 self = super(NSSpeechDriver, self).init() 15 if self: NameError: name 'objc' is not defined

Akul2010 commented 6 months ago

try doing pip install pyobjc==9.0.1

Akul2010 commented 6 months ago

also try making it in a separate env so that it doesn't conflict with global packages

ScottRojas commented 6 months ago

try doing pip install pyobjc==9.0.1

Same error in venv.

`Traceback (most recent call last): File "/Users/scottrojas/Desktop/T2S/t2s/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/scottrojas/Desktop/T2S/t2s/Text2Speech.py", line 3, in engine_object = pyttsx3.init() File "/Users/scottrojas/Desktop/T2S/t2s/lib/python3.9/site-packages/pyttsx3/init.py", line 22, in init eng = Engine(driverName, debug) File "/Users/scottrojas/Desktop/T2S/t2s/lib/python3.9/site-packages/pyttsx3/engine.py", line 30, in init self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug) File "/Users/scottrojas/Desktop/T2S/t2s/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 "", line 1030, in _gcd_import File "", line 1007, in _find_and_load File "", line 986, in _find_and_load_unlocked File "", line 680, in _load_unlocked File "", line 790, in exec_module File "", line 228, in _call_with_frames_removed File "/Users/scottrojas/Desktop/T2S/t2s/lib/python3.9/site-packages/pyttsx3/drivers/nsss.py", line 12, in class NSSpeechDriver(NSObject): File "/Users/scottrojas/Desktop/T2S/t2s/lib/python3.9/site-packages/pyttsx3/drivers/nsss.py", line 13, in NSSpeechDriver @objc.python_method NameError: name 'objc' is not defined`

ScottRojas commented 6 months ago

No issues on my Windows machine. It works as expected.

Akul2010 commented 6 months ago

It's a NSSS driver error for sure, but since I don't have a MacOS I can't help with those. I still try my best. I think it might be an error with the pyobjc library now, though. Can't upgrade to the new versions because they don't support all versions of Python 3. I might just copy the source in my rlvoice project or fork it to work according to the needs of this package. Better yet, if anyone knows of a replacement, that would be helpful.

holytony commented 6 months ago

try doing pip install pyobjc==9.0.1

I think you are right. I'm using M1 MacBook, I have a pytts3 project with pyobjc 9.0.1 running smoothly, the new one with pyobjc ==10.1 had the some problem, after changing pyobjc back to 9.0.1 manually, it works now.

I'm using python 3.10 and pyttsx3 2.88

Akul2010 commented 6 months ago

Yeah, if it's not detecting objc then it likely can't find the pyobjc package or can't read it idk.

willwade commented 2 months ago

Anyone else - check this fork.. https://github.com/thevickypedia/py3-tts