python - cx_freeze 不允许我使用 librosa Python3 制作图形用户界面

标签 python python-3.x cx-freeze librosa

你好 noob python 用户,我正在尝试使用 cx_freeze 和 librosa 音频库制作可执行文件。但是,每次我尝试使用 cx_freeze 制作可执行文件并导入 librosa 库时,可执行文件都不起作用。我能帮忙吗? 注意:主要代码只是调试错误的示例脚本。

这是主要代码,它是示例代码,但导入了 librosa。我正在使用这段代码 调试并输出相同的错误。

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

这是我的 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)])

错误图像 cx_Freeze: cx_Freeze error jpeg

错误图像pyinstaller Error Image Pyinstaller

最佳答案

我设法使用 pyinstaller 获得了一个功能可执行文件,但它需要做一些工作。无论出于何种原因,pyinstaller 就像完全忽略 librosa 的存在一样,即使使用 hidden-import 指定,所以我写了一个自定义钩子(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))

即便如此,我仍然缺少一个导入,但那个导入作为一个隐藏的导入。所以总的来说我得到了代码

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

使用命令构建功能可执行文件

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

关于python - cx_freeze 不允许我使用 librosa Python3 制作图形用户界面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66898490/

相关文章:

python - Ttk 主题设置

python-3.x - Pyinstaller .exe 找不到 _tiffile 模块 - 加载一些压缩图像会很慢

python - cx_freeze 无法捆绑 django.utils.six.moves

python - 通过 Python 将不同 URL 缩短服务的缩短 URL 转换回来

python - 从 Powerpoint 中提取表格

python - 使用 Python 将 .doc 转换为纯文本

python - cx_freeze : How do I resolve conflicts caused by multiple . 同名dylib文件

javascript - 如何在django中的python函数处理过程中显示动画图标?

python - 添加到同一个字典的不同生成器返回相同的值

python - 如何在 cxfreeze 安装程序中包含子目录中的文件