将带音乐的Python 3.6.1俄罗斯方块游戏打包为.exe遇阻求指导
Hey there! Let’s work through this packaging headache together—adding audio resources to a Python-to-exe build is one of the most common pain points, especially with an older Python version like 3.6.1. I’ve debugged plenty of these issues, so here’s a tailored guide for both PyInstaller and cx_Freeze that should get your Tetris game running as a standalone .exe.
First, Critical Pre-Checks
Before diving into tools, make sure you’ve covered these basics:
- Use relative paths for audio: In your game code, avoid hardcoding absolute paths (like
C:\Games\music.mp3). Instead, useos.pathto reference files relative to your script. For example:import os audio_path = os.path.join(os.path.dirname(__file__), "music.mp3") - Match tool versions to Python 3.6.1: Newer versions of PyInstaller/cx_Freeze drop support for older Python releases. Stick to:
- PyInstaller 3.4 or 3.5
- cx_Freeze 5.1.1
PyInstaller Step-by-Step Fix
PyInstaller often fails with audio because it doesn’t automatically include non-Python files. Here’s how to force it to bundle your music:
Generate a spec file: Instead of packing directly, create a configuration file first. Run this in your terminal:
pyi-makespec --onefile your_tetris_game.pyThis creates a
your_tetris_game.specfile.Edit the spec file to include audio: Open the spec file and find the
dataslist. Add your music file(s) here using the format(source_path, destination_folder). For example:datas=[ ("path/to/your/music.mp3", "."), # Puts music in the same folder as the exe # Add other audio files here if needed ]If your game uses pygame’s mixer, also add hidden imports to avoid missing modules:
hiddenimports=["pygame.mixer", "pygame.mixer_music"]Build using the spec file: Run this command to start the packaging process:
pyinstaller your_tetris_game.specPost-build check: Navigate to the
distfolder—your .exe and music file should be there. If the music still doesn’t play, run the exe from the command line to see error messages (this will tell you if the path is wrong or a module is missing).
cx_Freeze Step-by-Step Fix
cx_Freeze requires a setup script to define which files to include. Here’s a foolproof setup:
Create a
setup.pyfile: Save this in the same folder as your game script:import sys import os from cx_Freeze import setup, Executable # List all audio files you need to bundle include_files = ["music.mp3"] # Add more if you have sound effects # Configure the executable (use Win32GUI if you don't want a console window) base = "Win32GUI" if sys.platform == "win32" else None game_exe = Executable("your_tetris_game.py", base=base) setup( name="TetrisWithMusic", version="1.0", description="Your custom Tetris game", options={ "build_exe": { "include_files": include_files, "packages": ["pygame"] # Explicitly include your game's dependencies } }, executables=[game_exe] )Update your game’s audio path: Since cx_Freeze changes the working directory when running the exe, update your code to find the audio file correctly:
import sys import os if getattr(sys, 'frozen', False): # Running as bundled exe audio_path = os.path.join(sys._MEIPASS, "music.mp3") else: # Running as regular script audio_path = os.path.join(os.path.dirname(__file__), "music.mp3")Build the exe: Run this command in your terminal:
python setup.py buildYour bundled game will be in the
buildfolder under a subdirectory named after your OS and Python version.
Troubleshooting Common Issues
- Audio file missing in build: Double-check the
datas(PyInstaller) orinclude_files(cx_Freeze) lists—typos in file paths are the #1 culprit. - Module not found errors: Add missing packages to
hiddenimports(PyInstaller) orpackages(cx_Freeze). Running the exe from the command line will show exactly which module is missing. - Music plays but cuts out: Make sure your audio file is in a format that’s supported by your library (e.g., pygame works best with MP3/WAV). Avoid high-bitrate files that might cause loading issues.
内容的提问来源于stack exchange,提问作者šm98

