使用pyinstaller在Python中嵌入.wav文件 [英] Embed a .wav file in Python with pyinstaller

查看:52
本文介绍了使用pyinstaller在Python中嵌入.wav文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我正在运行的代码:

This is the code I am running:

import tkinter
from pygame import mixer
root = tkinter.Tk()
mixer.init()
mixer.music.load(r'C:\Users\George\AppData\Local\Programs\Python\Python36-32\Scripts\Music\Am_lie_-_JY_Suis_Jamais_All_-Yann_Tiersen.wav')
mixer.music.play()
root.mainloop()

我使用py2exe将其转换为windows .exe:

I convert this to windows .exe with py2exe giving:

pyinstaller -w -F -i "C:\Users\George\AppData\Local\Programs\Python\Python36-32\Scripts\test.ico" sound.py

我要做的是将py2exe编译后的wav文件嵌入python .exe中,这样,如果从Sound.exe本身之外的其他计算机上运行,​​则不需要该文件.

What I want to do is make the wav file embedded in the python .exe after it has been compiled by py2exe so that it does not need the file if run from a different computer aside from the Sound.exe itself.

这可能吗?

我对python完全陌生.

I am completely new to python.

我发现这段代码也许可以完成工作,但无法正常工作.

I found this code that maybe does the job but can't get it to work.

dfiles = [(".","Am_lie_-_JY_Suis_Jamais_All_-Yann_Tiersen.wav"])]

setup(
    windows=[{'script':'filename.py'}],
    data_files=dfiles,
    options={'py2exe':{'bundle_files':1,'compressed':1}}

感谢您的帮助.

推荐答案

似乎您使用的是PyInstaller,而不是py2exe.因此,此问题是相关的.

It looks like you're using PyInstaller, not py2exe. As such this question is relevant.

我修改了您的mcve示例,以使用相对路径加载我的wav文件.

I modified your mcve example to use a relative path to load my wav file.

import tkinter
from pygame import mixer
root = tkinter.Tk()
mixer.init()
mixer.music.load("recy.wav")
mixer.music.play()
root.mainloop()

然后,我将该数据文件包含在 pyinstaller 命令行中以构建可执行文件:

Then I included that data file in the pyinstaller command line to build the executable:

pyinstaller -w -F -i d_python.ico --add-data "recy.wav;." --log-level=WARN sound_test.py

文档-添加数据需要src和一个位置,在Windows上用; 隔开,在其他任何地方都用:隔开.在这里,我只是从本地目录中获取了它,并类似地将其存储"在了分布式应用程序的根目录中.

From the documentation, --add-data needs src and a location, separated by ; on Windows and : everywhere else. Here I've just grabbed it from the local directory and similarly 'stored' it in the root directory of the distributed app.

这对我有用,尽管一个文件(-F)选项有一点点负载开销.

This works for me, although the one-file (-F) option has a little bit of load overhead.

这篇关于使用pyinstaller在Python中嵌入.wav文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆