Pygame Community Edition
Pygame-ce (Pygame Community Edition) is an actively maintained, free and open-source Python library for multimedia applications and game development. It is a community-driven fork of the original Pygame, built on top of the Simple DirectMedia Layer (SDL) library. It offers frequent updates, bug fixes, performance enhancements, and improved compatibility with newer Python versions and operating systems. The current stable version is 2.5.7.
Common errors
-
ModuleNotFoundError: No module named 'pygame'
cause Pygame-ce is not installed, or the Python interpreter running the script does not have `pygame-ce` installed in its environment. This can happen with multiple Python installations or unactivated virtual environments.fixEnsure `pygame-ce` is installed for the correct Python interpreter using `pip install pygame-ce`. If using a virtual environment, activate it first. Verify installation with `pip list`. -
error: subprocess-exited-with-error × python setup.py egg_info did not run successfully.
cause This error during installation, especially on Linux or when building from source, often indicates missing system-level development dependencies for SDL or its components (e.g., `freetype2`).fixInstall required development packages for SDL and its dependencies. On Debian/Ubuntu: `sudo apt-get install build-essential libsdl2-dev libsdl2-image-dev libsdl2-mixer-dev libsdl2-ttf-dev libfreetype6-dev`. Refer to the `pygame-ce` compilation wiki for your specific OS. -
pygame.error: No available video device
cause This error occurs when Pygame attempts to initialize a display but cannot find a suitable video backend. This is common on servers, CI/CD pipelines, or other headless environments without a graphical display server. (General Pygame knowledge)fixFor headless environments, consider using a virtual display server like Xvfb (e.g., `xvfb-run python your_game.py`) or setting the SDL video driver to 'dummy' if a visual output is not strictly required (`os.environ['SDL_VIDEODRIVER'] = 'dummy'` before `pygame.init()`).
Warnings
- breaking Python 3.9 support was dropped in `pygame-ce 2.5.7`. PyPy 3.9 and 3.10 support was dropped in `pygame-ce 2.5.6`. Ensure your Python environment meets the `requires_python` specification (`>=3.10` for 2.5.7).
- breaking Pygame-ce is undergoing a significant migration to SDL3. While `2.5.x` versions aim for compatibility with SDL3 compilation, `pygame-ce 3.0` is planned to introduce breaking API changes related to this migration, including potential changes to mouse APIs and audio support.
- gotcha Installing `pygame-ce` while the original `pygame` package is already present can lead to conflicts, as both use the `pygame` import name. This often results in the original `pygame` being imported instead of `pygame-ce`.
Install
-
pip uninstall pygame # (if previously installed, to avoid package conflicts) -
pip install pygame-ce --upgrade
Imports
- pygame
import pygame
Quickstart
import pygame
import sys
pygame.init()
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
SCREEN_TITLE = 'Pygame-CE Quickstart'
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption(SCREEN_TITLE)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(BLUE) # Fill the screen with blue
# Example: Draw a white circle
pygame.draw.circle(screen, WHITE, (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2), 50)
pygame.display.flip() # Update the full display Surface to the screen
pygame.quit()
sys.exit()