Fix “No module named 'pygame'”

Python can't find Pygame because it was installed into a different Python than the one running your code. Here's how to find out which and fix it in a minute.

2 min readUpdated
On this page

Why it happens#

Traceback (most recent call last):
  File "game.py", line 1, in <module>
    import pygame
ModuleNotFoundError: No module named 'pygame'

Python packages are installed per Python interpreter. Most computers have more than one: a system Python, one from python.org, one from the Microsoft Store or Homebrew, and virtual environments. pip and your editor can each point at a different one. The error means the Python running your script doesn't have Pygame, even if another one does.

The fix: install into the Python that runs your code#

Step 1. Ask the failing Python where it lives. Run this the same way you run your game (same terminal, same editor button):

Python
import sys
print(sys.executable)

Step 2. Install Pygame using that exact interpreter:

Terminal
"C:\path\shown\above\python.exe" -m pip install pygame       # Windows
/path/shown/above/python -m pip install pygame               # macOS / Linux

Using python -m pip instead of plain pip guarantees pip installs into that interpreter. Run your game again.

Fixes for specific setups#

VS Code#

Open the command palette (Ctrl+Shift+P), run Python: Select Interpreter and choose the interpreter you installed Pygame into, or your project's .venv. The selected interpreter is shown in the status bar. The Run button and the integrated terminal then use it.

PyCharm#

Go to Settings → Project → Python Interpreter. Either select the interpreter that has Pygame, or click + in the packages list and install pygame there.

IDLE#

IDLE runs with the Python it belongs to. Open a terminal and install with that version, for example py -3.13 -m pip install pygame on Windows if you opened IDLE for Python 3.13.

Virtual environments#

If you created a .venv, activate it before installing and before running:

Terminal
source .venv/bin/activate     # macOS/Linux
.venv\Scripts\activate        # Windows
python -m pip install pygame
python game.py

Windows with several Pythons#

py -0 lists every installed version. py -3.13 -m pip install pygame installs for 3.13 specifically.

Other causes#

  • The install actually failed. Scroll up in the pip output. If it ended with an error rather than Successfully installed, see pip install errors.
  • A file named pygame.py in your folder. Python imports it instead of the library. This usually causes AttributeError rather than ModuleNotFoundError, but rename it anyway and delete __pycache__.
  • Using python2. Pygame 2 needs Python 3. Run python3 game.py.
  • pygame-ce users: the package is called pygame-ce for pip but you still write import pygame. Don't write import pygame_ce.

Check it's fixed#

Terminal
python -c "import pygame, sys; print(pygame.version.ver, sys.executable)"
↑ ↓ to moveEnter to openEsc to close