在虚拟环境中使用pyttsx3和python的声音问题

时间:2019-10-31 04:43:17

标签: python virtualenv pyttsx

因此,基本上我是在虚拟环境中构建python虚拟助手软件,我想使用pyttsx3将文本转换为语音。第一个问题是,如果我运行pyttsx3.init()会收到错误

o = self.data[key]()
KeyError: None

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File ".\__init__.py", line 3, in <module>
    init_speech()
  File "C:\Users\DJETHA\eclipse-workspace\PythonFull\PythonProjects\Betax\index.py", line 31, in init_speech
    engine = pyttsx3.init()
  File "C:\Users\DJETHA\eclipse-workspace\PythonFull\PythonProjects\Betax\venv\lib\site-packages\pyttsx3\__init__.py", line 46, in init
    eng = Engine(driverName, debug)
  File "C:\Users\DJETHA\eclipse-workspace\PythonFull\PythonProjects\Betax\venv\lib\site-packages\pyttsx3\engine.py", line 52, in __init__
    self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug)
  File "C:\Users\DJETHA\eclipse-workspace\PythonFull\PythonProjects\Betax\venv\lib\site-packages\pyttsx3\driver.py", line 75, in __init__
    self._module = importlib.import_module(name)
  File "C:\Users\DJETHA\eclipse-workspace\PythonFull\PythonProjects\Betax\venv\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\DJETHA\eclipse-workspace\PythonFull\PythonProjects\Betax\venv\lib\site-packages\pyttsx3\drivers\sapi5.py", line 3, in <module>
    import win32com.client
  File "C:\Users\DJETHA\eclipse-workspace\PythonFull\PythonProjects\Betax\venv\lib\site-packages\win32com\__init__.py", line 5, in <module>
    import win32api, sys, os

进行研究后,我发现使用pyttsx3.init(“ dummy”)不会产生任何错误。但是,下一个问题是当我运行

engine =pyttsx3.init("dummy")
engine.say("hey")
engine.runAndWait()

我什么也没听到或看到任何错误

我应该注意,如果我在虚拟环境之外的pyttsx3.init中使用上面的代码时没有虚拟对象,那么一切正常。

2 个答案:

答案 0 :(得分:0)

尝试一下:

import pyttsx3
from pyttsx3.drivers import sapi5

engine = pyttsx3.init('sapi5')
engine.say('hey')
engine.runAndWait()

答案 1 :(得分:0)

我在 1 个月前遇到了同样的错误。 我犯的错误是我最初安装了 python X36 版本。这就是原因。 检查python架构使用下面的代码

src\4-4-project>python
>>import platform
>>platform.architecture()

如果输出显示 32 位而不是 64 位,则卸载 python X36bit 并全新安装 python X64。 不过不要担心预装的软件包。您只需通过以下代码保存所有包

pip freeze > packages.txt
#or
python -m pip freeze> packages.txt

安装python x64bit版本后,您可以通过以下代码重新安装所有软件包

python -m pip install -r packages.txt

设置python x64bit版本并设置路径后,下面的代码就足够了。不要忘记再次安装软件包。

import pyttsx3
engine = pyttsx3.init()
engine.say("hello world")
engine.runAndWait()

希望它有效。