Pyinstaller does not include libsndfile - python-3.x

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

Related

pip broken, can't use most of pip commands (on windows)

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.

pyenv BUILD FAILED while trying (pyenv install -v 3.9.0) on wsl2 ubuntu 20.04

SOLVED: the solution was I needed zlib1g-dev
This is the error pyenv produces
Traceback (most recent call last):
File "<frozen zipimport>", line 520, in _get_decompress_func
ModuleNotFoundError: No module named 'zlib'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<frozen zipimport>", line 568, in _get_data
File "<frozen zipimport>", line 523, in _get_decompress_func
zipimport.ZipImportError: can't decompress data; zlib not available
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<string>", line 6, in <module>
File "/tmp/python-build.20201212114143.1082/Python-3.9.0/Lib/runpy.py", line 206, in run_module
mod_name, mod_spec, code = _get_module_details(mod_name)
File "/tmp/python-build.20201212114143.1082/Python-3.9.0/Lib/runpy.py", line 147, in _get_module_details
return _get_module_details(pkg_main_name, error)
File "/tmp/python-build.20201212114143.1082/Python-3.9.0/Lib/runpy.py", line 111, in _get_module_details
__import__(pkg_name)
File "<frozen zipimport>", line 241, in load_module
File "<frozen zipimport>", line 709, in _get_module_code
File "<frozen zipimport>", line 570, in _get_data
zipimport.ZipImportError: can't decompress data; zlib not available
Traceback (most recent call last):
File "/tmp/python-build.20201212114143.1082/Python-3.9.0/Lib/runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/tmp/python-build.20201212114143.1082/Python-3.9.0/Lib/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/tmp/python-build.20201212114143.1082/Python-3.9.0/Lib/ensurepip/__main__.py", line 5, in <module>
sys.exit(ensurepip._main())
File "/tmp/python-build.20201212114143.1082/Python-3.9.0/Lib/ensurepip/__init__.py", line 210, in _main
return _bootstrap(
File "/tmp/python-build.20201212114143.1082/Python-3.9.0/Lib/ensurepip/__init__.py", line 129, in _bootstrap
return _run_pip(args + [p[0] for p in _PROJECTS], additional_paths)
File "/tmp/python-build.20201212114143.1082/Python-3.9.0/Lib/ensurepip/__init__.py", line 38, in _run_pip
return subprocess.run([sys.executable, "-c", code], check=True).returncode
File "/tmp/python-build.20201212114143.1082/Python-3.9.0/Lib/subprocess.py", line 524, in run
raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['/tmp/python-build.20201212114143.1082/Python-3.9.0/python', '-c', '\nimport runpy\nimport sys\nsys.path = [\'/tmp/tmpk898xfhs/setuptools-49.2.1-py3-none-any.whl\', \'/tmp/tmpk898xfhs/pip-20.2.3-py2.py3-none-any.whl\'] + sys.path\nsys.argv[1:] = [\'install\', \'--no-cache-dir\', \'--no-index\', \'--find-links\', \'/tmp/tmpk898xfhs\', \'--root\', \'/\', \'--upgrade\', \'setuptools\', \'pip\']\nrunpy.run_module("pip", run_name="__main__", alter_sys=True)\n']' returned non-zero exit status 1.
make: *** [Makefile:1254: install] Error 1
I have found nothing on this when googling and have no idea what is going on.
the steps that I have done are, first installing the pyenv installer and running that script then trying to run (pyenv install -v 3.9.0)
the system does have python3.8 installed and pyenv does seem to know about it
but it still will not let me install 3.9.0
EDIT: I should also throw in that in order to let pyenv know about the system version of python I did this (sudo ln -s /usr/bin/python3 /usr/bin/python).
Another thing is I also tried installing 3.9.0 with a tool called asdf and that resulted in the same error
EDIT2: It seems like im missing zlib from the traceback but Im on ubuntu and do have zlib1g installed and from what I looked up thats what zlib is on ubuntu 20.04 im going to try and find more info on the zlib and see if I need to install something else
I had a similar problem trying to install Python 3.8 with asdf (which uses the same backend as pyenv) on WSL2/Ubutnu18, and installing zlib1g-dev was not enough to solve it as stated by the question's author.
Therefore, as a more general solution, I have found that installing all dependencies recommended by pyenv for Ubuntu did solve the problem for asdf (and I suspect for pyenv as well). The dependencies for different OS can be found here: https://github.com/pyenv/pyenv/wiki#suggested-build-environment
I hope this is useful for others!
PS: This resource might be useful as well for similar issues: https://github.com/pyenv/pyenv/wiki/Common-build-problems

Problem creating executable pygame - pyinstaller

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

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

Resources