简体   繁体   English

使用pyinstaller在Python中嵌入.wav文件

[英]Embed a .wav file in Python with pyinstaller

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

I convert this to windows .exe with py2exe giving: 我把它转换为带有py2exe的windows .exe给出:

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

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执行编译之后将wav文件嵌入到python .exe中,这样如果从Sound.exe本身以外的其他计算机运行它就不需要该文件。

Is this possible? 这可能吗?

I am completely new to python. 我是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}}

Any help is appreciated. 任何帮助表示赞赏。

It looks like you're using PyInstaller, not py2exe. 看起来你正在使用PyInstaller而不是py2exe。 As such this question is relevant. 因此, 这个问题是相关的。

I modified your mcve example to use a relative path to load my wav file. 我修改了你的mcve示例以使用相对路径来加载我的wav文件。

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

Then I included that data file in the pyinstaller command line to build the executable: 然后我在pyinstaller命令行中包含该数据文件以构建可执行文件:

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

From the documentation , --add-data needs src and a location, separated by ; 文档中 , - --add-data需要src和一个位置,以;分隔; on Windows and : everywhere else. 在Windows上和:其他地方。 Here I've just grabbed it from the local directory and similarly 'stored' it in the root directory of the distributed app. 在这里,我只是从本地目录中抓取它,并类似地“存储”它在分布式应用程序的根目录中。

This works for me, although the one-file (-F) option has a little bit of load overhead. 这对我有用,虽然单文件(-F)选项有一点负载开销。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM