当我编译以下脚本时:
# 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")
使用下面的setup.py:
from cx_Freeze import setup, Executable
exe = Executable(
script="play.py",
)
setup(
executables = [exe]
)
通过:python setup.py build
执行play.exe会出现以下错误:
Traceback (most recent call last):
File "C:Python33libsite-packagescx_FreezeinitscriptsConsole3.py", line 2
7, in <module>
exec(code, m.__dict__)
File "play.py", line 7, in <module>
pygame.error: Couldn't open 'song.ogg'
脚本在编译前工作得很好,是的,我确实放了歌曲。exe目录下的Ogg 。顺便说一下歌曲。ogg工作正常,我已经检查过了。什么好主意吗?
注:如果我将其更改为song.wav,它可以正常工作,但WAV文件对我来说太大了。MP3也不能正常工作
通过进程资源管理器,我发现我需要从Python33Libsite-packagespygame中复制libogg.dll, libvorbisis .dll和libvorbisfile.dll到我冻结的程序目录。
电脑无法打开歌曲。因为cx_freeze在编译时没有包含这首歌。您应该尝试将歌曲文件包含到setup.py
脚本中。使用如下所示的setup.py
脚本
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]
)
根据您的回答,我找到了mac用户的解决方案(假设您使用Homebrew安装pygame):
brew install libogg
brew install libvorbis
brew install sdl_mixer --with-libvorbis
如果您在运行第三个命令后收到"Warning: sol_mixer-1.2.12 already installed."的消息,则将其替换为以下内容:
brew reinstall sdl_mixer --with-libvorbis
参考:不能在mac上使用pygame打开声音文件