问题
When I compile the following script:
# play.py
import os, re
import pygame.mixer
pygame.mixer.init(11025)
pygame.mixer.music.load('song.ogg')
pygame.mixer.music.play(-1)
os.system("PAUSE")
Using the following setup.py:
from cx_Freeze import setup, Executable
exe = Executable(
script="play.py",
)
setup(
executables = [exe]
)
Through:
python setup.py build
Executing play.exe gives me the following error:
Traceback (most recent call last):
File "C:\Python33\lib\site-packages\cx_Freeze\initscripts\Console3.py", line 2
7, in <module>
exec(code, m.__dict__)
File "play.py", line 7, in <module>
pygame.error: Couldn't open 'song.ogg'
The script works fine before compiling and yes, I did put song.ogg in exe's directory. By the way song.ogg works fine, I already checked. Any ideas?
P.S. If I change it to song.wav it works fine, but WAV files are way too large for me to use. Also MP3 doesn't work as it should.
回答1:
Through Process Explorer I was able to find out I needed to copy libogg.dll, libvorbis.dll and libvorbisfile.dll from Python33\Lib\site-packages\pygame to my frozen program's directory.
回答2:
The computer cant open the song.ogg file because cx_freeze did not include the song in it's compiling. You should try including the song file into the setup.py
script. Using a setup.py
script like the one below.
from cx_Freeze import setup, Executable
exe=Executable(
script="play.py",
base="Win32Gui",
)
includefiles=[song.ogg]
includes=[]
excludes=[]
packages=[]
setup(
version = "0.0",
description = "Description",
author = "Name",
name = "Play",
options = {'build_exe': {'excludes':excludes,'packages':packages,'include_files':includefiles}},
executables = [exe]
)
回答3:
Expanding on your answer, I found the this solution for mac users (assuming you used Homebrew to install pygame):
brew install libogg
brew install libvorbis
brew install sdl_mixer --with-libvorbis
If you receive a message saying "Warning: sol_mixer-1.2.12 already installed." after running the 3rd command, then replace it with the following:
brew reinstall sdl_mixer --with-libvorbis
For reference: can't open sound files using pygame on a mac
来源:https://stackoverflow.com/questions/15755921/cant-run-pygame-script-that-plays-a-ogg-file-compiled-through-cx-freeze