no module named 'pygame' (or any other module)

We can install external Python modules using the pip command. To use it, we have to add it to the PATH. PATH is an environment variable in operating systems like Windows, macOS, and Linux that specifies directories where executable programs are located. It allows the system to find and run programs without needing their full file paths.

Instructions for the Windows operating system.

  1. Search for "Environment Variables" in the Start menu and click on Edit the system environment variables.
  2. Click on the Environment Variables button in the System Properties window.
  3. Under System variables, locate the PATH variable and select it.
  4. Click Edit and add the path to the python.exe file on your computer.
  5. Click OK to save changes and close all dialogs.
  6. Restart your computer to apply the changes (it isn't always necessary).
  7. Install the required module using pip install moduleName in CMD.
Warning: If you have several versions of Python on your computer, beware that if you add the .exe file for, e.g., Python 3.9 to the PATH, the installed modules won't work in the Python 3.12 IDLE.