I get this error from the exe generated by PyInstaller when using Pyttsx3. The code works fine in python. I've tried using other versions of PyInstaller and Pyttsx but it doesn't make a difference. I've also tried Py2exe which is also not working with Pyttsx3, does anyone know what's causing this?
The code
import pyttsx3
engine = pyttsx3.init()
engine.say('Test')
engine.runAndWait()
The error after running the exe generated
Traceback (most recent call last):
File "site-packages\pyttsx3\__init__.py", line 44, in init
File "c:\python34\lib\weakref.py", line 125, in __getitem__
o = self.data[key]()
KeyError: None
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "Test.py", line 85, in <module>
File "site-packages\pyttsx3\__init__.py", line 46, in init
File "site-packages\pyttsx3\engine.py", line 52, in __init__
File "site-packages\pyttsx3\driver.py", line 75, in __init__
File "importlib\__init__.py", line 109, in import_module
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2212, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2224, in _find_and_load_unlocked
ImportError: No module named 'pyttsx3.drivers'
Try this:
import pyttsx3
from pyttsx3.drivers import sapi5
engine = pyttsx3.init()
engine.say('Test')
engine.runAndWait()
Explanation:
You actually need to import an extra module from pyttsx3.
Goto location:
C:\Users\username\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\PyInstaller\hooks
Create a newfile "hook-pyttsx3.py"
inside 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', ]
Now your program will run without getting error.
Click here Github issue created
Try this:
pyinstaller --hidden-import=pyttsx3.drivers --hidden-import=pyttsx3.drivers.dummy --hidden-import=pyttsx3.drivers.espeak --hidden-import=pyttsx3.drivers.nsss --hidden-import=pyttsx3.drivers.sapi5
Hidden import argument for pyinstaller import 3 rd party packages into build. By adding above lines to pyinstaller will create a spec file with hidden-import =[‘pyttsx3.drivers’,’pyttsx3.drivers.dummy’,....] which will rectify the error “no module named pyttsx.driver’ but eventually u will end up other error also which i am unable to solve.
I've just fixed pyttsx3 compatibility in #101. In a couple of weeks you will be able to:
pip install "pyinstaller-hooks-contrib>=2021.2"
but until then you can use the Github version:
pip install -U https://github.com/pyinstaller/pyinstaller-hooks-contrib/archive/refs/heads/master.zip
Add the --clean option the first time you run PyInstaller after using pip (unless you're using auto-py-to-exe which blocks PyInstaller's caching). It should then work on all platforms without using any --hiddenimport-ing.
Related
So I was working with python, coding and installing packages, everything was going ok, but then everything with pip stoped working, and every times the cmd prompt me :
Traceback (most recent call last):
File "c:\python39\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "c:\python39\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\Python39\Scripts\pip.exe\__main__.py", line 7, in <module>
File "c:\python39\lib\site-packages\pip\_internal\cli\main.py", line 69, in main
command = create_command(cmd_name, isolated=("--isolated" in cmd_args))
File "c:\python39\lib\site-packages\pip\_internal\commands\__init__.py", line 91, in create_command
module = importlib.import_module(module_path)
File "c:\python39\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 855, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "c:\python39\lib\site-packages\pip\_internal\commands\list.py", line 9, in <module>
from pip._internal.cli.req_command import IndexGroupCommand
File "c:\python39\lib\site-packages\pip\_internal\cli\req_command.py", line 20, in <module>
from pip._internal.index.collector import LinkCollector
File "c:\python39\lib\site-packages\pip\_internal\index\collector.py", line 5, in <module>
import cgi
File "c:\python39\lib\cgi.py", line 41, in <module>
import html
ModuleNotFoundError: No module named 'html'
I never run into those kind of errors.
BTW pip -V is working, and when I try to run a programme using python with the already installed package and it still works fine.
I tried to reinstall python partially after restarting my pc, and nothing with pip has changed
I fixed it, here is what I have done, and more context that may help you understand where the problem was coming from :
So I was doing an gui app using pyqt5 and I was having trouble with PySide2, the first error of this kind was when I tried to install it with pip (pip install PySide2). I don't know where the error came from, but if you encounter this after in a similar situation, there way be a problem with that.
what I have done to fix it was not re-installing python or pip but using the repair "tool" when installing the version of python already installed.
I am trying to get a screenshot utility to work after porting it from:
PyQt4 -> PyQt5
Python 2.7 -> Python 3.7
Ubuntu -> Windows 7
original code
new ported code
I changed all the Qt4 references over to Qt5 and got it mostly working except for the Select Area function that lets you select an area on the screen by drawing a rectangle. This part fails with the error:
resArea: Traceback (most recent call last):
File "screenshot.py", line 318, in <module>
d = display.Display()
File "C:\Users\frak\AppData\Local\Programs\Python\Python37\lib\site-packages\python_xlib-0.24-py3.7.egg\Xlib\display.py", line 89, in __init__
File "C:\Users\frak\AppData\Local\Programs\Python\Python37\lib\site-packages\python_xlib-0.24-py3.7.egg\Xlib\display.py", line 71, in __init__
File "C:\Users\frak\AppData\Local\Programs\Python\Python37\lib\site-packages\python_xlib-0.24-py3.7.egg\Xlib\protocol\display.py", line 85, in __init__
File "C:\Users\frak\AppData\Local\Programs\Python\Python37\lib\site-packages\python_xlib-0.24-py3.7.egg\Xlib\support\connect.py", line 72, in get_display
File "C:\Users\frak\AppData\Local\Programs\Python\Python37\lib\site-packages\python_xlib-0.24-py3.7.egg\Xlib\support\connect.py", line 55, in _relative_import
File "C:\Users\frak\AppData\Local\Programs\Python\Python37\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 668, in _load_unlocked
File "<frozen importlib._bootstrap>", line 638, in _load_backward_compatible
File "C:\Users\frak\AppData\Local\Programs\Python\Python37\lib\site-packages\python_xlib-0.24-py3.7.egg\Xlib\support\unix_connect.py", line 31, in <module>
ModuleNotFoundError: No module named 'fcntl'
I know that fcntl isn't supported on Windows but I think there could be a workaround since I think what the code is trying to do is get some screen dimensions before proceeding.
All the research I've done so far points me to other things like pyscreenshot or alternative implementations that only do a full-screen grab but don't have a rectangular selector to select a screen region.
I like this implementation because it shows you a quick preview and options to select an area, adjust delay, hide window and save to file.
Any help would be greatly appreciated
I recently began work on a game for my daughter to help with learning words. I am moving along and decided to create the executable file (I'll done this with many other games). This game is somewhat different due to sounds and music. I have tried everything I can think of, searched everything I can think of etc. This is the error CMD reports, the error is referring to a sound file. I have tried adding the file directly with --add-data I have tried placing the executable in the same directory as the sound file (which shouldn't be needed since it should bundle it already). The script runs absolutely fine otherwise (from CMD etc.) ANY IDEAS??
C:\Users\launc\Desktop\Coding\Games\g_sight_words\dist>sight_words.exe
pygame 1.9.4
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "sight_words.py", line 5, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "c:\users\launc\appdata\local\programs\python\python37-32\lib\site-
packages\PyInstaller\loader\pyimod03_importers.py", line 627, in
exec_module
exec(bytecode, module.__dict__)
File "settings.py", line 21, in <module>
pygame.error: Unable to open file 'the.wav'
[9892] Failed to execute script sight_words
What does your .spec file look like? Here's the PyInstaller docs on adding data files.
Basically you need to add something like:
a = Analysis(...
datas=[ ('the.wav', '.') ],
...
)
This will put your sound file ('the.wav') into the root directory of your compiled application (the second argument, '.')
Then in your application you can check if you're running from source or as a compiled executable. I use a helper function:
def my_path(path_name):
"""Return the appropriate path for data files based on execution context"""
if getattr( sys, 'frozen', False ):
# running in a bundle
return(os.path.join(sys._MEIPASS, path_name))
else:
# running live
return path_name
So then your application code will look something like:
the_sound = pygame.mixer.Sound(my_path("the.wav"))
I created an executable with this code and pyinstaller:
import soundfile
print("Hello!")
input("Ok")
But it doesn't run correctly. I get the following error:
Traceback (most recent call last):
File "lib\site-packages\soundfile.py", line 142, in <module>
OSError: sndfile library not found
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "prueba.py", line 1, in <module>
import soundfile
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "C:\Users\DianaCarolina\Google Drive\Humboldt\DTF_GUI\venv1\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
exec(bytecode, module.__dict__)
File "lib\site-packages\soundfile.py", line 163, in <module>
OSError: cannot load library 'C:\Users\DianaCarolina\Google Drive\Humboldt\DTF_GUI\dist\prueba\_soundfile_data\libsndfile32bit.dll': error 0x7e
[9780] Failed to execute script prueba
Since I used the standard pyinstaller command pyinstaller prueba.py I was able to make the executable work by copying the original _soundfile_data folder in the dist folder. However, I would like to use the --onefile option to make my program easier to distribute. How can I make pyinstaller work as it is supposed to?
add as external hook like this
pyinstaller <your parameters> --hidden-import='package.module'
You can use the --add-binary option in cases like this. For example:
--add-binary /path/to/_soundfile_data/libsndfile.dylib:_soundfile_data
In my case I find the (active) conda environment pysndfile is installed into and get libsndfile out of that: --add-binary $(dirname $(which python))/../lib/python3.7/site-packages/_soundfile_data/libsndfile.dylib:_soundfile_data
I have created the exe by using py2exe and this works fine in windows 8 , Application window runs without python installing. But same distribution or exe gives an error in windows 7 and windows XP. Below is the traceback for the same.
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\PySide\_utils.py", line 93, in get_pyside_dir
File "<loader>", line 10, in <module>
File "<loader>", line 8, in __load
ImportError: (DLL load failed: The specified module could not be found.) 'C:\\Users\\Test\\Desktop\\123\\dist\\PySide.QtCore.pyd'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "TopicMapParser.py", line 11, in <module>
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1191, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1161, in _load_backward_compatible
File "C:\Python34\lib\site-packages\PySide\__init__.py", line 41, in <module>
File "C:\Python34\lib\site-packages\PySide\__init__.py", line 11, in _setupQtDirectories
File "C:\Python34\lib\site-packages\PySide\_utils.py", line 95, in get_pyside_dir
File "C:\Python34\lib\site-packages\PySide\_utils.py", line 88, in _get_win32_case_sensitive_name
File "C:\Python34\lib\site-packages\PySide\_utils.py", line 63, in _get_win32_short_name
FileNotFoundError: [WinError 3] The system cannot find the path specified.
Please help on this issue.
We are shooting into the dark when you don't post your code. So please, next time, please post your code.
Look at the errors. Python has excellent error handling and naming conventions to make it really clear on whats going wrong. It can't find the module that you are requesting. Have you attempted to update your python? To attempt to find the module that you are attempting to import? Have you double checked to make sure you did not misspell the module name? Have you searched around on stackoverflow for existing similar questions?
You are getting an ImportError and FileNotFoundError meaning it is not installed, file path is broken or you misspelled the module name.
It seems like you are trying to install QtCore, which is a package that you need to manually install using sudo apt-get install python-qt4. For windows, you have to install something that allows you to do apt-get since it is a Unix/Linux command. Try using wuinstall.
I have tried to generate the exe by using Py2exe in Python 3.4. It creates the exe and it works fine in the same machine. But it was giving an error in other machine. I have also tried with cx-freeze , still I was not able to resolve the issue. But I installed the Pyside in Python 2.7 and used py2exe for the same version and it worked nicely.
Below is the code that I have used in setup.py.
from distutils.core import setup
import py2exe
data = [('', [r'hpXMLTools.ico']), ('imageformats',[r'C:\Python34\Lib\site-packages\PySide\plugins\imageformats\qico4.dll'])]
setup(windows=[{'script': 'TopicMapParser.py',
'icon_resources': [(1, 'hpXMLTools.ico')]
}],
data_files=data,
options={
'py2exe':
{
'optimize': 2
}
}, requires=['PySide', 'xlsxwriter'])