AttributeError: module 'pyttsx3' has no attribute 'init'

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-05 08:04:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!