3

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

NickyWicky
  • 31
  • 3
  • personally, I've had a much smoother experience using pyinstaller than cx_freeze, but the only suggestion I might give is that you may need to explicitly add some modules to "packages" that are imported by your dependencies – csunday95 Apr 01 '21 at 05:10
  • [Similar problem including circular import with numba and llvmlite](https://stackoverflow.com/questions/66821044/circular-dependency-while-executing-cx-freeze-result). It might be related to non-matching versions of libs, such as in this numba comment: [Your llvmlite<... dependencies introduce circular dependency issues on upgrade. The old numba requires llmvmlite<0.34.0, therefore llvmlite is not upgraded, while the upgrade is necessary to upgrade numba. Always have to force the upgrades with ...](https://aur.archlinux.org/packages/python-numba/?comments=all&O=10&PP=10). – Lenka Čížková Apr 02 '21 at 22:46
  • @csunday95 So before cx_freeze, I was using PyInstaller which I forgot to mention. Pyinstaller/Auto-Py-To-Exe won't work for librosa either, cx somewhat gave me more information on the issue. I also tried to explicity add some packages but, the same issue came out. – NickyWicky Apr 04 '21 at 00:16
  • Does this answer your question? [Circular dependency while executing cx\_Freeze result](https://stackoverflow.com/questions/66821044/circular-dependency-while-executing-cx-freeze-result) – jpeg Apr 19 '21 at 11:46

1 Answers1

1

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

csunday95
  • 1,279
  • 10
  • 18
  • *I deleted previous comment* So I went to another computer and fresh installed everything on my laptop. I am starting to receive a distribution error after trying to run the exe and the distribution error is in regards to PyInstaller... any possible insight? I linked the new error above. – NickyWicky Apr 06 '21 at 05:20
  • so you got the error posted above after building a program with pyinstaller on one computer, then moving the resulting executable to another computer? Was it exactly the code in the original question, or some new code? What command line command did you run to build (something similar to the last line of my answer)? – csunday95 Apr 06 '21 at 18:50