1

I had a program (using module pyttsx3) written in Windows which I used to run on python3.7, and it used to run well.
But,when I try to run it on Ubuntu 20.04, it doesn't run.
I have both python3.8.2 and python3.7.7 installed on my Ubuntu 20.04 PC, running the program with python3.8.2 interpreter gives the following errors:

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

On the other hand, running it with python3.7 interpreter gives the following ModuleNotFoundError:

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

...but, when I check the installed modules using the command pip3 list, pyttsx3 shows up in the list with version: 2.87

Now, I am clueless!...dunno what to do.


UPDATE-1 (pipenv)

I tried running it with pipenv using the following commands as suggested in the answer by @ivan-borshchov :

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

...but this gives the following errors which are somewhat similar to the errors I get when using python3.8.2 interpreter.

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

UPDATE-2 (pipenv run python3.7 ...)

Okay while fiddling with pipenv, I found that running python3.7 in pipenv is again giving the ModuleNotFoundError, the same as I get when running my program in python3.7 without pipenv (Even though, I have installed pyttsx3 in pipenv as mentioned in UPDATE-1 section).

So, then I am curious whether it is possible to install pyttsx3 in python3.7 in pipenv(rather than in python3, in pipenv).

Martin Medro
  • 338
  • 3
  • 12
  • 2
    Having multiple versions of Python installed like this is usually a recipe for disaster. Use virtual environments (e.g. Anaconda) if you really need more than one Python. It looks like you've installed pyttsx3 into your Py3.8 environment (which is what pip3 is showing you) but not into your Py3.7 environment. As for your 3.8 error (`KeyError: None`) it looks like `driverName` has not been set. Possibly it did not install properly the first time – Carl H May 12 '20 at 14:21

2 Answers2

1

Try run your project in pipenv:

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

It will give you a clear environment so will allow track issue easier without the influences of another interpreters

-1

First go to the location:

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

Create a new file hook-pyttsx3.py.

Inside this file, copy the code below.

#-----------------------------------------------------------------------------
# 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', ]

Then, in your script, use this:

import pyttsx3
from pyttsx3.drivers import sapi5

reference

MarianD
  • 1,008
  • 2
  • 10
  • 15