Skip to content

0.6 Packages, virtual environments and pygame-ce

A Level; IGCSE at Part 1DPart 0

Suggested time: 1 lesson · Syllabus: 9618 11.1 (built-in functions and library routines) · 9.1 (abstraction)

What you will do

Install pygame-ce, the graphics, sound and input library used by every game series, inside a virtual environment, and run a setup check. By the end of this chapter you will be able to:

  • explain what a library and a package manager are;
  • use pip to install a library;
  • create, activate and leave a virtual environment;
  • record a project's libraries in requirements.txt;
  • run a series' check_setup.py.

Key vocabulary

Term Meaning Example
Library Ready-written, tested code you can use in your own program pygame
Module A single Python file or package that can be imported random, os
pip Python's package manager: it downloads libraries from the Python Package Index (PyPI) python3 -m pip install pygame-ce
Dependency A library that another piece of software needs The games depend on pygame-ce
Virtual environment A private set of libraries for one project, kept separate from every other project .venv
requirements.txt A file listing the libraries a project needs pygame-ce==2.5.8

The big idea: standing on the shoulders of libraries

Drawing a picture in a window sounds simple, but the operating system must create the window, pixels must reach the graphics card, key presses must be detected and sounds must reach the speakers, and every operating system does these things differently. Pygame has already solved these problems. Your program calls simple functions such as pygame.display.set_mode() and Pygame deals with the details.

Concept: libraries and abstraction

How it works: a library exposes an interface (the functions you may call and what they do) and hides its implementation (how it does them). import pygame loads the library so you can call pygame.something().
Why we use it: libraries save a huge amount of time and are usually more reliable than code you'd write yourself, because many people have used and tested them. Using one is a form of abstraction: you think "draw this image here", not "copy these bytes into video memory".
Where else it's used: almost every real program uses libraries. In your own projects, say which parts you wrote and which came from a library.

Why pygame-ce?

There are two versions of Pygame:

  • pygame: the original project;
  • pygame-ce (Community Edition): a fork maintained by former Pygame developers and updated more often.

Both are used the same way: you still write import pygame. The game series use pygame-ce. When they were written (September 2026), the latest original pygame release did not provide ready-built installers for the newest Python versions, while pygame-ce did. That may have changed, so check each project's page on pypi.org if you're unsure.

Never install both

Both install a package called pygame, so they conflict. If you already have pygame, remove it first with python3 -m pip uninstall pygame (Windows: py -m pip uninstall pygame).

Step 1: Create a virtual environment

Make a folder for your graphical projects, cd into it, then:

py -m venv .venv
.venv\Scripts\activate
python3 -m venv .venv
source .venv/bin/activate

When it is active, your prompt starts with (.venv). Inside an active environment, python means the environment's Python on every system, so the commands below use python.

Concept: virtual environments

How it works: a virtual environment is a folder (here .venv) with its own place to store libraries. When it is activated, python and pip use that folder instead of the system-wide one.
Why we use it: different projects often need different versions of the same library. Without separate environments you could only have one installed. They also stop you damaging the system's own Python, which is why some systems refuse pip install without one.
Where else it's used: every professional Python project uses them, and other languages have the same idea (JavaScript keeps each project's libraries in a node_modules folder).

PowerShell may block activate

In PowerShell the command is .venv\Scripts\Activate.ps1, and some computers block it with a security setting. If that happens, use Command Prompt instead, or ask your teacher.

Step 2: Install pygame-ce

python -m pip install pygame-ce

You should see pip download a file and finish with Successfully installed pygame-ce-….

What's happening? Why -m pip?

python -m pip means "use the pip that belongs to this Python". If a computer has more than one Python, plain pip might install the library for a different one from the one your editor uses. -m pip avoids that.

Check it:

python -m pip show pygame-ce
python -c "import pygame"
python -m pygame.examples.aliens

The second command prints one line like pygame-ce 2.x.x (SDL 2.x.x, Python 3.x.x); pygame-ce prints this itself whenever it's imported. The third runs a small demo game that comes with Pygame, which is a good check that graphics and sound work. Stop it with Ctrl+C or by closing its window.

Using Thonny?

Thonny has its own Python. Install libraries with Tools → Manage packages: search for pygame-ce and click Install.

Step 3: Run a series' setup check

Every game series starts with a stage_01 folder containing check_setup.py. Unzip a code pack (for example Rivet Rush/Rivet_Rush_code_pack.zip), cd into code/stage_01, and run it:

python check_setup.py

A window opens for five seconds saying Pygame is working!, with a sprite from the game. The terminal shows something like:

Output
Python version: 3.14.0
pygame-ce 2.5.8 (SDL 2.32.10, Python 3.14.0)
Pygame version: 2.5.8
Sprite loaded from …/code/stage_01/assets/images/player_idle.png
Setup check finished. You are ready for Stage 2!

Your version numbers may differ, and that's fine.

Checkpoint

You have finished this chapter when the test window opens and the terminal says You are ready for Stage 2!

Step 4: Record what the project needs

python -m pip freeze > requirements.txt

pip freeze lists every library in the environment with its exact version, and > saves that list to a file. Anyone can then rebuild the same environment with:

python -m pip install -r requirements.txt

The book's own build uses exactly this: see book/requirements.txt in the course repository.

Step 5: Leaving and coming back

deactivate

turns the environment off. You need to activate it again each time you open a new terminal. In VS Code, choose the interpreter inside .venv (Python: Select Interpreter) and VS Code will use it automatically.

Don't put .venv in Git

The environment can be rebuilt from requirements.txt, and it's large. Chapter 0.7 shows how to tell Git to ignore it.

Troubleshooting

What you see What to try
ModuleNotFoundError: No module named 'pygame' pygame-ce is installed for a different Python from the one running your code. Activate the environment, or in VS Code check the interpreter in the bottom bar, or in Thonny use Manage packages.
pip prints a long error about building wheel or setup.py You probably typed pygame instead of pygame-ce on a new Python. Install pygame-ce.
error: externally-managed-environment Your system won't let pip change its own Python. Use a virtual environment (Step 1).
Could not find … player_idle.png The assets folder isn't next to check_setup.py, or you are in the wrong folder (Chapter 0.4, Step 3).
The prompt doesn't show (.venv) The environment isn't active. Run the activate command again from the folder that contains .venv.

Check your understanding

  1. What is the difference between a library and a module in Python?
  2. Why is python3 -m pip install pygame-ce safer than pip install pygame-ce on a computer with more than one Python?
  3. Give two advantages of using a library such as Pygame rather than writing the same code yourself, and one possible disadvantage.
  4. What is a virtual environment, and why might a developer working on two projects need one?
Answers
  1. A module is a single importable file or package. A library is a collection of ready-written code, often made of several modules, designed to be reused.
  2. -m pip runs the pip belonging to that particular Python, so the library is installed where that Python will look for it.
  3. Advantages, for example: saves time; already tested by many users; handles differences between operating systems. Disadvantages, for example: you depend on someone else's updates; it may be larger than you need; you have less control over how it works.
  4. A private set of libraries for one project. The two projects might need different versions of the same library, which can't both be installed in one place.

Summary

  • A library is ready-made, tested code; pip installs libraries from PyPI.
  • Always use python -m pip, so the library goes to the right Python.
  • Create a virtual environment per project with -m venv .venv, and activate it in each new terminal.
  • Install pygame-ce (never both it and pygame), then run a series' check_setup.py.
  • pip freeze > requirements.txt records what a project needs; pip install -r requirements.txt rebuilds it.
This is a free sample: log in for every book →