AttributeError:模块“pyttsx3”没有属性“init”

2024-10-03 11:21:56 发布

您现在位置:Python中文网/ 问答频道 /正文

首先,我使用:

  • Windows 10
  • Python3.6.2(但我也尝试过Python3.5.4)
  • pyttsx3模块

我试图使用pyttsx3,但是我不能用官方的代码示例初始化它。在

我的代码(就像herehere中的示例一样):

import pyttsx3
engine = pyttsx3.init()
engine.say('Just a sample text.')
engine.runAndWait()

第二行给出了一个错误:

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

我用PIP安装了它:

^{pr2}$

我试图在安装pypiwin32时修复它,但它仍然不起作用:

pip install pypiwin32

当我执行以下脚本时:

import pyttsx3
print(dir(pyttsx3))

我明白了:

['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'pyttsx3']

有这样一个:

drivers (folder)
__pycache__ (folder)
driver.py
engine.py
six.py
voice.py
__init__.py

地址:

C:\Program Files\Python36\Lib\site-packages\pyttsx3

以及文件__init__.py的内容(我省略了注释):

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

Tags: 代码pyimport示例hereinitfoldereng