简体   繁体   中英

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:

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.

Is this possible?

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

Any help is appreciated.

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

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

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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