问题
First of all, I'm using:
- Windows 10
- Python 3.6.2 (but I've tried with Python 3.5.4 too)
- pyttsx3 module
I'm trying to use pyttsx3 but I just can't initialize it, with the official code examples.
My code (just like the examples from here and here):
import pyttsx3
engine = pyttsx3.init()
engine.say('Just a sample text.')
engine.runAndWait()
And the second line gives me this error:
AttributeError: module 'pyttsx3' has no attribute 'init'
I installed it with PIP:
pip install pyttsx3
And I tried to fix it installing pypiwin32 but it still doesn't work:
pip install pypiwin32
When I execute the following script:
import pyttsx3
print(dir(pyttsx3))
I get this:
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'pyttsx3']
There is this:
drivers (folder)
__pycache__ (folder)
driver.py
engine.py
six.py
voice.py
__init__.py
At:
C:\Program Files\Python36\Lib\site-packages\pyttsx3
And the contents of the file __init__.py
(I omitted comments):
from .engine import Engine
import weakref
_activeEngines = weakref.WeakValueDictionary()
def init(driverName=None, debug=False):
try:
eng = _activeEngines[driverName]
except KeyError:
eng = Engine(driverName, debug)
_activeEngines[driverName] = eng
return eng
回答1:
It appears that the module pyttsx3 is not properly initialised. I hope you don't have a file named pyttsx3.py anywhere in the module path. I found a related issue here.
回答2:
Don't name your file or other file pyttsx3.py, I don't know why but thats the problem
来源:https://stackoverflow.com/questions/46863651/attributeerror-module-pyttsx3-has-no-attribute-init