cx_freeze isn't allowing me to make a gui with librosa Python3

Viewed 126

Hello noob python user here, I am trying to make an executable using cx_freeze and librosa audio library. However every time I attempt to make the executable with cx_freeze and import the librosa library, the executable does not work. Could I have some help with this? Note: Main code is just an example script to debugg error.

Here is main code which is example code but importing librosa. I am using this code to just debug and it outputs the same error.

import PySimpleGUI as sg
import librosa
import IPython as ipd
sg.theme('DarkAmber')   # Add a little color to your windows
# All the stuff inside your window. This is the PSG magic code compactor...
layout = [  [sg.Text('Some text on Row 1')],
            [sg.Text('Enter something on Row 2'), sg.InputText()],
            [sg.OK(), sg.Cancel()]]

# Create the Window
window = sg.Window('Window Title', layout)
# Event Loop to process "events"
while True:             
    event, values = window.read()
    if event in (sg.WIN_CLOSED, 'Cancel'):
        break

window.close()

Here is my setup file for cx_Freeze

import sys
from cx_Freeze import setup, Executable

# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["os"], "excludes": [] }

# GUI applications require a different base on Windows (the default is for
# a console application).
base = None
if sys.platform == "win32":
    base = "Win32GUI"

setup(  name = "Billy_Boy",
        version = "01",
        description = "My GUI application!",
        options = {"build_exe": build_exe_options},
        executables = [Executable("NACD_Work.py", base=base)])

Error Image cx_Freeze: cx_Freeze error jpeg

Error Image pyinstaller Error Image Pyinstaller

1 Answers

I managed to get a functional executable with pyinstaller, but it took some doing. For whatever reason, pyinstaller just feels like completely ignoring the existence of librosa, even when specified with hidden-import, so I wrote a custom hook:


import os.path
import glob
from PyInstaller.compat import EXTENSION_SUFFIXES
from PyInstaller.utils.hooks import collect_data_files, get_module_file_attribute


datas = collect_data_files('librosa')
librosa_dir = os.path.dirname(get_module_file_attribute('librosa'))
for ext in EXTENSION_SUFFIXES:
    ffimods = glob.glob(os.path.join(librosa_dir, '_lib', f'*_cffi_*{ext}*'))
    dest_dir = os.path.join('librosa', '_lib')
    for f in ffimods:
        binaries.append((f, dest_dir))

Even with this, I still had a missing import, but that one worked as a hidden import. So overall I got the code

import PySimpleGUI as sg
import librosa
import numpy as np
import sys


def main(args):
    test_data = np.zeros(10000)
    test_data[::50] = 1
    print(librosa.beat.tempo(test_data))

    sg.theme('DarkAmber')   # Add a little color to your windows
    # All the stuff inside your window. This is the PSG magic code compactor...
    layout = [  [sg.Text('Some text on Row 1')],
                [sg.Text('Enter something on Row 2'), sg.InputText()],
                [sg.OK(), sg.Cancel()]]

    # Create the Window
    window = sg.Window('Window Title', layout)
    # Event Loop to process "events"
    while True:             
        event, values = window.read()
        if event in (sg.WIN_CLOSED, 'Cancel'):
            break

    window.close()
    return 0


if __name__ == '__main__':
    sys.exit(main(sys.argv))

to build into a functional executable with the command

pyinstaller -F --hidden-import librosa --additional-hooks-dir . --hidden-import sklearn.utils._weight_vector .\test.py

Related