问题描述
你好,这里的 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
解决方法
我设法使用 pyinstaller 获得了一个功能性的可执行文件,但需要做一些事情。不管什么原因,pyinstaller 感觉就像完全忽略了 librosa 的存在,即使是用 hidden-import
指定的,所以我写了一个自定义钩子:
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