Problem creating executable pygame - pyinstaller - audio

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"))

Related

Pyinstaller does not include libsndfile

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

Pyttsx3 not working with PyInstaller

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.

Module missing error

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'])

How to assort files of an executable python3 program made with cx_freeze?

I developed a project in python 3.4.1 and pyqt4. Then I converted my python program to executable file using cx_freeze module.
Everything is perfectly fine, but I found two problems here:
Size of program gets really high.
I like to assort my files in special folders by their format type, for example all of the .dll files in a folder called DLL and something like that, but when I do this I ran into some problems.
Is it really possible ? If yes give me some information and if no, what can I do to solve my problems. Thanks
Error I get :
cx_Freeze: Python error in main script
Traceback (most recent call last): File
"C:\Python34\lib\site-packages\cx_Freeze\initscripts\Console.py", line
27, in
exec(code, m.dict) File "MY APP.py", line 1, in File "c:\Python\64-bit\3.4\lib\importlib_bootstrap.py", line 2237, in
_find_and_load File "c:\Python\64-bit\3.4\lib\importlib_bootstrap.py", line 2226, in
_find_and_load_unlocked File "c:\Python\64-bit\3.4\lib\importlib_bootstrap.py", line 1191, in
_load_unlocked File "c:\Python\64-bit\3.4\lib\importlib_bootstrap.py", line 1161, in
_load_backward_compatible File "ExtensionLoader_PyQt4_QtCore.py", line 22, in File "ExtensionLoader_PyQt4_QtCore.py", line
21, in bootstrap ImportError: No module named PyQt4.QtCore

cx_freeze doesn't work in my script

I freeze a script using cx_freeze (4.3.1) in python 3.4.
The script works perfectly running in IDLE. It uses tkinter, re, and reportlab to create a pdf form.
Then the following error occurrs when I run the exe.
PS C:\Python34\build\exe.win32-3.4> .\CREEPING_cx_freeze.exe
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\cx_Freeze\initscripts\Console.py", line 27, in <module>
exec(code, m.__dict__)
File "CREEPING_cx_freeze.py", line 6, in <module>
File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 2214, in _find_and_load
File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 2203, in _find_and_load_unlocked
File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 1191, in _load_unlocked
File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 1161, in _load_backward_compatible
AttributeError: 'module' object has no attribute '_fix_up_module'
It looks like everything is working. Everything is created in the build folder, but the compiled executable does not work.
I tried searching for a solution but did not find any. Could somebody help me with this?
I´m using win 8 64 bits
Wrong version installed.
pip installs 64 bits but it doesn,t work.
running the 32bits version...works

Resources