Turn your Pygame game into an executable with PyInstaller
Players shouldn't need Python installed to play your game. PyInstaller bundles your code, Python and Pygame into an app you can share.
On this page
PyInstaller analyses your program, collects every module it imports, and bundles them with a copy of the Python interpreter into a folder or a single executable. It works on Windows, macOS and Linux, but builds only for the system it runs on: build on Windows for Windows players, on a Mac for Mac players.
Install and do a first build#
Work inside your game's virtual environment so only the packages your game needs are bundled:
python -m pip install pyinstaller
pyinstaller --windowed main.py--windowed (also spelled --noconsole) stops a terminal window from opening next to your game on Windows and macOS. The result appears in dist/main/. Run the executable inside to test it.
One folder or one file?#
- One folder (the default) creates a folder with the executable and its supporting files. It starts quickly and is the most reliable choice. Zip the folder to share it. In PyInstaller 6 the supporting files live in an
_internalsubfolder next to the executable. - One file (
--onefile) creates a single executable. It's convenient, but each launch unpacks everything to a temporary folder first, so it starts more slowly.
pyinstaller --onefile --windowed --name StarCatcher main.pyBundling images, sounds and fonts#
PyInstaller bundles Python code automatically but not your data files. Add them with --add-data SOURCE:DEST:
pyinstaller --windowed --add-data "assets:assets" main.pyPyInstaller 6 accepts the colon separator on every platform; older versions on Windows needed a semicolon (assets;assets). You can repeat --add-data for several folders.
Finding bundled files at runtime#
In a one-file build your files are unpacked to a temporary folder whose path is stored in sys._MEIPASS. A small helper finds files whether you run from source or from the bundle:
import sys
from pathlib import Path
def resource_path(relative):
base = Path(getattr(sys, "_MEIPASS", Path(__file__).parent))
return base / relative
player_image = pygame.image.load(resource_path("assets/player.png")).convert_alpha()Never write save files next to the executable. Use a per-user folder, as explained in saving game data.
Icons and names#
pyinstaller --windowed --onefile --name StarCatcher --icon icon.ico main.py # Windows
pyinstaller --windowed --name StarCatcher --icon icon.icns main.py # macOSAlso set the in-game window icon with pygame.display.set_icon().
Common problems#
- The game opens and closes immediately: build without
--windowedonce so you can read the error in the console, usually a missing asset path. FileNotFoundErrorfor assets: useresource_path()and check your--add-dataarguments.- Antivirus warnings: one-file executables made by PyInstaller are sometimes flagged by mistake. One-folder builds and code signing reduce this. Don't ask players to disable their antivirus.
- Huge files: build in a clean virtual environment so unrelated packages aren't bundled.
- macOS "app is damaged" or "unidentified developer": apps downloaded from the internet must be signed and notarised with an Apple Developer account to open without warnings.
Licensing#
Your bundle includes Pygame (LGPL-2.1) and the SDL libraries it uses. Include their license texts with your game, and remember that the LGPL lets players replace the library; the one-folder build makes that straightforward. See licenses. This is general information, not legal advice.
For web releases instead of downloads, see publishing to the web with pygbag.