python3.7:(Pyttsx3)没有名为“pyttsx3”的模块

python3.7:(Pyttsx3)没有名为“pyttsx3”的模块

我有一个用 Windows 编写的程序(使用模块 pyttsx3),我曾经在 python3.7 上运行它,它曾经运行良好。
但是,当我尝试在 Ubuntu 20.04 上运行它时,它无法运行。
我的 Ubuntu 20.04 PC 上安装了 python3.8.2 和 python3.7.7,使用 python3.8.2 解释器运行该程序会出现以下错误:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/pyttsx3/__init__.py", line 20, in init
    eng = _activeEngines[driverName]
  File "/usr/lib/python3.8/weakref.py", line 131, in __getitem__
    o = self.data[key]()
KeyError: None

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/aakash/ax-net/home/a/ax-thrusters/axon/proice.py", line 19, in <module>
    core("I am ready", "a", "b")
  File "/home/aakash/ax-net/home/a/ax-thrusters/axon/proice.py", line 17, in core
    voice.core(x, id)
  File "/home/aakash/ax-net/home/a/ax-thrusters/axon/voice.py", line 15, in core
    engine = pyttsx3.init()
  File "/usr/local/lib/python3.8/dist-packages/pyttsx3/__init__.py", line 22, in init
    eng = Engine(driverName, debug)
  File "/usr/local/lib/python3.8/dist-packages/pyttsx3/engine.py", line 30, in __init__
    self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug)
  File "/usr/local/lib/python3.8/dist-packages/pyttsx3/driver.py", line 50, in __init__
    self._module = importlib.import_module(name)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/usr/local/lib/python3.8/dist-packages/pyttsx3/drivers/espeak.py", line 9, in <module>
    from . import _espeak, toUtf8, fromUtf8
  File "/usr/local/lib/python3.8/dist-packages/pyttsx3/drivers/_espeak.py", line 18, in <module>
    dll = cdll.LoadLibrary('libespeak.so.1')
  File "/usr/lib/python3.8/ctypes/__init__.py", line 451, in LoadLibrary
    return self._dlltype(name)
  File "/usr/lib/python3.8/ctypes/__init__.py", line 373, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: libespeak.so.1: cannot open shared object file: No such file or directory

另一方面,使用 python3.7 解释器运行它会出现以下 ModuleNotFoundError:

.
.
.
import pyttsx3
ModuleNotFoundError: No module named 'pyttsx3'

...但是,当我使用命令检查已安装的模块时pip3 list,pyttsx3 出现在列表中,版本为:2.87

现在,我很茫然!...不知道该怎么办。


更新-1 (pipenv)

我尝试pipenv使用以下命令运行它,如@ivan-borshchov 在回答中所建议的那样:

pipenv --three
pipenv install pyttsx3
pipenv run python3 MyPythonFile.py

...但是这会出现以下错误,这些错误与我使用 python3.8.2 解释器时遇到的错误有些类似。

Traceback (most recent call last):
  File "/home/aakash/.local/share/virtualenvs/axon-aTooHeXl/lib/python3.8/site-packages/pyttsx3/__init__.py", line 20, in init
    eng = _activeEngines[driverName]
  File "/usr/lib/python3.8/weakref.py", line 131, in __getitem__
    o = self.data[key]()
KeyError: None

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "axon.py", line 6, in <module>
    import proice
  File "/home/aakash/ax-net/home/a/ax-thrusters/axon/proice.py", line 19, in <module>
    core("I am ready", "a", "b")
  File "/home/aakash/ax-net/home/a/ax-thrusters/axon/proice.py", line 17, in core
    voice.core(x, id)
  File "/home/aakash/ax-net/home/a/ax-thrusters/axon/voice.py", line 15, in core
    engine = pyttsx3.init()
  File "/home/aakash/.local/share/virtualenvs/axon-aTooHeXl/lib/python3.8/site-packages/pyttsx3/__init__.py", line 22, in init
    eng = Engine(driverName, debug)
  File "/home/aakash/.local/share/virtualenvs/axon-aTooHeXl/lib/python3.8/site-packages/pyttsx3/engine.py", line 30, in __init__
    self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug)
  File "/home/aakash/.local/share/virtualenvs/axon-aTooHeXl/lib/python3.8/site-packages/pyttsx3/driver.py", line 50, in __init__
    self._module = importlib.import_module(name)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/home/aakash/.local/share/virtualenvs/axon-aTooHeXl/lib/python3.8/site-packages/pyttsx3/drivers/espeak.py", line 9, in <module>
    from . import _espeak, toUtf8, fromUtf8
  File "/home/aakash/.local/share/virtualenvs/axon-aTooHeXl/lib/python3.8/site-packages/pyttsx3/drivers/_espeak.py", line 18, in <module>
    dll = cdll.LoadLibrary('libespeak.so.1')
  File "/usr/lib/python3.8/ctypes/__init__.py", line 451, in LoadLibrary
    return self._dlltype(name)
  File "/usr/lib/python3.8/ctypes/__init__.py", line 373, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: libespeak.so.1: cannot open shared object file: No such file or directory

更新-2(pipenv 运行 python3.7...)

好吧,在摆弄的时候pipenv,我发现在 pipenv 中运行 python3.7 再次出现了 ModuleNotFoundError,与我在没有 pipenv 的情况下在 python3.7 中运行程序时遇到的一样(尽管我已经在 pipenv 中安装了 pyttsx3,如 UPDATE-1 部分所述)。

所以,我很好奇是否可以在 pipenv 中的 python3.7 中安装 pyttsx3(而不是在 pipenv 中的 python3 中)。

答案1

尝试在以下位置运行您的项目pipenv

pipenv --three
pipenv install pyttsx3 
pipenv run python3 yourfile.py

它将为您提供一个清晰的环境,因此可以更轻松地跟踪问题,而不会受到其他解释器的影响

答案2

首先前往地点:

C:\Users\username\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\PyInstaller\hooks 

创建一个新文件hook-pyttsx3.py

在此文件中,复制以下代码。

#-----------------------------------------------------------------------------
# Copyright (c) 2013-2020, PyInstaller Development Team.
#
# Distributed under the terms of the GNU General Public License (version 2
# or later) with exception for distributing the bootloader.
#
# The full license is in the file COPYING.txt, distributed with this software.
#
# SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
#-----------------------------------------------------------------------------


""" pyttsx3 imports drivers module based on specific platform. Fount at https://github.com/nateshmbhat/pyttsx3/issues/6 """

hiddenimports = [
    'pyttsx3.drivers',
    'pyttsx3.drivers.dummy',
    'pyttsx3.drivers.espeak',
    'pyttsx3.drivers.nsss',
    'pyttsx3.drivers.sapi5', ]

然后,在您的脚本中使用此脚本:

import pyttsx3
from pyttsx3.drivers import sapi5

参考

相关内容