I ran into the same error a few days ago! Thankfully, I found the answer.
You see, the problem is that pygame comes in a .whl (wheel) file/package. So, as a result, you have to pip install it.
Pip installing is a very tricky process, so please be careful. The steps are:-
Step1. Go to C:/Python (whatever version you are using)/Scripts. Scroll down. If you see a file named pip.exe, then that means that you are in the right folder. Copy the path.
Step2. In your computer, search for Environment Variables. You should see an option labeled ‘Edit the System Environment Variables’. Click on it.
Step3. There, you should see a dialogue box appear. Click ‘Environment Variables’. Click on ‘Path’. Then, click ‘New’. Paste the path that you copied earlier.
Step4. Click ‘Ok’.
Step5. Shift + Right Click wherever your pygame is installed. Select ‘Open Command Window Here’ from the dropdown menu. Type in ‘pip install py’ then click tab and the full file name should fill in. Then, press Enter, and you’re ready to go! Now you shouldn’t get the error again!!!
This is my situation:
I tried importing pygame in both python 3.4.2 and python 3.6.3 using both pip and pip3 respectively.
In the python 3.4.2 shell:
Traceback (most recent call last) is:
File «», line 1, in
import pygame
ImportError: No module named ‘pygame’
In the python 3.6.3 shell:
Traceback (most recent call last):
File «», line 1, in
import pygame
File «C:Usersaditya dandAppDataLocalProgramsPythonPython36libsite-packagespygame__init__.py», line 141, in
from pygame.base import *
ModuleNotFoundError: No module named ‘pygame.base’
Those are the errors that occurred.
I also used pygame-1.9.2a0-hg_5974ff8dae3c%2B.win32-py3.4.msi.
It’s showing the header’s file of pygame, but it’s not importing something.
What can I do to solve this?
Win10, Spyder5, Anaconda3.
import pygame as pg приводит к сообщению об ошибке ModuleNotFoundError: No module named ‘pygame’
Модуль был загружен (pip install pygame)
В списке pip list (Anaconda) этот модуль присутствует.
Других версий Python на компьютере нет.
-
Вопрос заданболее года назад
-
561 просмотр
Пригласить эксперта
перейдите в папку python/scripts, откройте командное окно к этому пути, введите следующее:
C:python34scripts> python -m pip install pygame
Чтобы проверить это, откройте python IDE и введите
import pygame
print (pygame.ver)
Это сработало для меня…
-
Показать ещё
Загружается…
24 мая 2023, в 21:54
100 руб./за проект
24 мая 2023, в 21:53
3000 руб./за проект
24 мая 2023, в 21:51
1 руб./за проект
Минуточку внимания
A common error you may encounter when using Python is modulenotfounderror: no module named ‘pygame’. This error occurs when Python cannot detect the pygame library in your current environment, and Pygame does not come with the default Python installation. This tutorial goes through the exact steps to troubleshoot this error for the Windows, Mac and Linux operating systems.
ModuleNotFoundError: no module named ‘pygame’
What is ModuleNotFoundError?
The ModuleNotFoundError occurs when the module you want to use is not present in your Python environment. There are several causes of the modulenotfounderror:
The module’s name is incorrect, in which case you have to check the name of the module you tried to import. Let’s try to import the re module with a double e to see what happens:
import ree
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
1 import ree
ModuleNotFoundError: No module named 'ree'
To solve this error, ensure the module name is correct. Let’s look at the revised code:
import re
print(re.__version__)
2.2.1
You may want to import a local module file, but the module is not in the same directory. Let’s look at an example package with a script and a local module to import. Let’s look at the following steps to perform from your terminal:
mkdir example_package
cd example_package
mkdir folder_1
cd folder_1
vi module.py
Note that we use Vim to create the module.py file in this example. You can use your preferred file editor, such as Emacs or Atom. In module.py, we will import the re module and define a simple function that prints the re version:
import re
def print_re_version():
print(re.__version__)
Close the module.py, then complete the following commands from your terminal:
cd ../
vi script.py
Inside script.py, we will try to import the module we created.
import module
if __name__ == '__main__':
mod.print_re_version()
Let’s run python script.py from the terminal to see what happens:
Traceback (most recent call last):
File "script.py", line 1, in <module>
import module
ModuleNotFoundError: No module named 'module'
To solve this error, we need to point to the correct path to module.py, which is inside folder_1. Let’s look at the revised code:
import folder_1.module as mod
if __name__ == '__main__':
mod.print_re_version()
When we run python script.py, we will get the following result:
2.2.1
Lastly, you can encounter the modulenotfounderror when you import a module that is not present in your Python environment.
What is pygame?
Pygame (stylized as pygame) is a set of Python modules for writing video games. It is highly portable and runs on every platform and operating system, and Pygame does not automatically come installed with Python. The simplest way to install pygame is to use the package manager for Python called pip. The following instructions to install pygame are for the major Python version 3.
How to install pygame on Windows Operating System
First, you need to download and install Python on your PC. Ensure you select the install launcher for all users and Add Python to PATH checkboxes. The latter ensures the interpreter is in the execution path. Pip is automatically on Windows for Python versions 2.7.9+ and 3.4+.
You can check your Python version with the following command:
python3 --version
You can install pip on Windows by downloading the installation package, opening the command line and launching the installer. You can install pip via the CMD prompt by running the following command.
python get-pip.py
You may need to run the command prompt as administrator. Check whether the installation has been successful by typing.
pip --version
To install pygame with pip, run the following command from the command prompt.
pip3 install pygame
How to install pygame on Mac Operating System
Open a terminal by pressing command (⌘) + Space Bar to open the Spotlight search. Type in terminal and press enter. To get pip, first ensure you have installed Python3. You can use the package manager Homebrew to do this. To install Homebrew, run the following command from your terminal:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
Follow the steps and prompts before the installation starts, then insert the Homebrew directory at the top of your PATH environment variable. You can do this by adding the following line at the bottom of your ~/.profile file:
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
Alternatively, if you have OS X 10.12 (Sierra) or older, use:
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
You can check that you have Python 3 installed on your system by running:
python3 --version
Python 3.8.8
Download pip by running the following curl command:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
The curl command allows you to specify a direct download link, and using the -o option sets the name of the downloaded file.
Install pip by running:
python3 get-pip.py
From the terminal, use pip3 to install pygame:
pip3 install pygame
How to install pygame on Linux Operating System
All major Linux distributions have Python installed by default. However, you will need to install pip. You can install pip from the terminal, but the installation instructions depend on the Linux distribution you are using. You will need root privileges to install pip. Open a terminal and use the commands relevant to your Linux distribution to install pip.
Installing pip for Ubuntu, Debian, and Linux Mint
sudo apt install python-pip3
Installing pip for CentOS 8 (and newer), Fedora, and Red Hat
sudo dnf install python-pip3
Installing pip for CentOS 6 and 7, and older versions of Red Hat
sudo yum install epel-release
sudo yum install python-pip3
Installing pip for Arch Linux and Manjaro
sudo pacman -S python-pip
Installing pip for OpenSUSE
sudo zypper python3-pip
Once you have installed pip, you can install pygame using:
pip3 install pygame
Check pygame Version
Once you have successfully installed pygame, you can use two methods to check the version of pygame. First, you can use pip show from your terminal.
pip show pygame
Name: pygame
Version: 2.1.2
Summary: Python Game Development
Second, within your python program, you can import pygame and then reference the __version__ attribute:
import pygame
print(pygame.__version__)
2.1.2
Installing pygame Using Anaconda
Anaconda is a distribution of Python and R for scientific computing and data science. You can install Anaconda by going to the installation instructions. Once you have installed Anaconda, you can install pygame using the following command:
conda install -c cogsci pygame
Summary
Congratulations on reading to the end of this tutorial. The modulenotfounderror occurs if you misspell the module name, incorrectly point to the module path or do not have the module installed in your Python environment. If you do not have the module installed in your Python environment, you can use pip to install the package. However, you must ensure you have pip installed on your system. You can also install Anaconda on your system and use the conda install command to install the pygame library.
For further reading on installing data science and machine learning libraries, you can go to the articles:
- OpenCV: How to Solve Python ModuleNotFoundError: no module named ‘cv2’
- Requests: How to Solve Python ModuleNotFoundError: no module named ‘requests’
- Pandas: How to Solve Python ModuleNotFoundError: no module named ‘pandas’
- Matplotlib: How to Solve Python ModuleNotFoundError: no module named ‘matplotlib’
- Flask: How to Solve Python ModuleNotFoundError: no module named ‘flask’
Go to the online courses page on Python to learn more about Python for data science and machine learning.
Have fun and happy researching!
One error that you might encounter when working with Python is:
ModuleNotFoundError: No module named 'pygame'
This error occurs when Python can’t find the pygame module in your current Python environment.
This tutorial shows examples that cause this error and how to fix it.
How to reproduce the error
Suppose you want to use the pygame module to develop a video game using Python.
You import the pygame module in your code as follows:
import pygame
pygame.init()
But you get the following error when running the code:
Traceback (most recent call last):
File "main.py", line 1, in <module>
import pygame
ModuleNotFoundError: No module named 'pygame'
This error occurs because the pygame module is not a built-in Python module, so you need to install it before using it.
To see if you have the pygame module installed, you can run the pip show pygame command from the terminal as follows:
$ pip3 show pygame
WARNING: Package(s) not found: pygame
If you get the warning as shown above, then you need to install the pygame module.
How to fix this error
To resolve this error, you need to install the pygame library using pip as shown below:
pip install pygame
# For pip3:
pip3 install pygame
Once the module is installed, you should be able to run the code that imports pygame without receiving the error.
Install commands for other environments
The install command might differ depending on what environment you used to run the Python code.
Here’s a list of common install commands in popular Python environments to install the pygame module:
# if you don't have pip in your PATH:
python -m pip install pygame
python3 -m pip install pygame
# Windows
py -m pip install pygame
# apt-get mirror for Ubuntu/Debian/Mint
sudo apt-get install python3-pygame
# yum mirror for CentOS/Fedora/Red hat
sudo yum install python3-pygame
Once the module is installed, you should be able to run the code without receiving this error.
Other common causes for this error
If you still see the error even after installing the module, it means that the pygame module can’t be found in your Python environment.
There are several reasons why this error can happen:
- You may have multiple versions of Python installed on your system, and you are using a different version of Python than the one where pygame is installed.
- You might have pygame installed in a virtual environment, and you are not activating the virtual environment before running your code.
- Your IDE uses a different version of Python from the one that has pygame
Let’s see how to fix these errors in practice.
1. You have multiple versions of Python
If you have multiple versions of Python installed on your system, you need to make sure that you are using the specific version where the pygame module is available.
You can test this by running the which -a python or which -a python3 command from the terminal:
$ which -a python3
/opt/homebrew/bin/python3
/usr/bin/python3
In the example above, there are two versions of Python installed on /opt/homebrew/bin/python3 and /usr/bin/python3.
Suppose you run the following steps in your project:
- Install pygame with
pipusing/usr/bin/Python version - Install Python using Homebrew, you have Python in
/opt/homebrew/ - Then you run
import pygamein your code
The steps above will cause the error because pygame is installed in /usr/bin/, and your code is probably executed using Python from /opt/homebrew/ path.
To solve this error, you need to run the pip install pygame command again so that pygame is installed and accessible by the active Python version.
2. Python virtual environment is active
Another scenario that could cause this error is you may have pygame installed in a virtual environment.
Python venv package allows you to create a virtual environment where you can install different versions of packages required by your project.
If you are installing pygame inside a virtual environment, then the module won’t be accessible outside of that environment.
You can see if a virtual environment is active or not by looking at your prompt in the terminal.
When a virtual environment is active, the name of that environment will be shown inside parentheses as shown below:
In the picture above, the name of the virtual environment (demoenv) appears, indicating that the virtual environment is currently active.
If you run pip install while the virtual environment is active, then the package is installed only for that environment
Likewise, any package installed outside of that virtual environment won’t be accessible from the virtual environment. The solution is to run the pip install command on the environment you want to use.
If you want to install pygame globally, then turn off the virtual environment by running the deactivate command before running the pip install command.
3. IDE using a different Python version
Finally, the IDE from where you run your Python code may use a different Python version when you have multiple versions installed.
For example, you can check the Python interpreter used in VSCode by opening the command palette (CTRL + Shift + P for Windows and ⌘ + Shift + P for Mac) then run the Python: Select Interpreter command.
You should see all available Python versions listed as follows:
You need to use the same version where you installed pygame so that the module can be found when you run the code from VSCode.
Once done, you should be able to import pygame into your code.
Conclusion
In summary, the ModuleNotFoundError: No module named 'pygame' error occurs when the pygame library is not available in your Python environment. To fix this error, you need to install pygame using pip.
If you already have the module installed, make sure you are using the correct version of Python, check if the virtual environment is active if you have one, and check for the Python version used by your IDE.
By following these steps, you should be able to import the pygame module in your code successfully.
I hope this tutorial is helpful. Until next time! 👋

