Как исправить ошибку modulenotfounderror no module named pygame

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:

enter image description here

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?

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!


Posted by Marta on March 15, 2023

Viewed 41167 times

Card image cap

In this article I will show you why you might encounter the error modulenotfounderror: no module named ‘pygame’ when you are creating a game using the python library pygame. You will also learn how to fix this error so you can continue working on your game.

The pygame module provides functionality to create a graphical display, work with graphics, and read from input devices, like mouse and keyboard, on multiple platforms. 

This module is a third-party module; therefore, the Python installation doesn’t include it by default. As a result, you need to install it separately. It is quite simple to install; you can find how in one of the sections below. Without further to do, let’s get started! 

How to check pygame is installed.

Probably you are facing the following situation. You wrote a small program using pygame, included all imports, installed python; however, when you execute the program, you see the error:

Traceback (most recent call last):
  File line 1, in <module>
    import random, pygame, sys
ModuleNotFoundError: No module named 'pygame'

This error means that the pygame module is not installed. Another option is that you have several python versions installed on your machine. It could be installed in a different python installation(python SDK) and not installed in the python installation you are using to execute the game.

Therefore, the first thing to tackle this problem is finding out if the module is installed. Whether you are using Mac or Windows, you can check if a module is installed using the pip tool. Pip is the package manager that python uses to keep track of all libraries installed. To check the list of installed libraries, you can run the following:

Output:

py==1.8.1
pyasn1==0.4.5
pygame==1.9.4
pygubu==0.9.8.6
pyparsing==2.4.7
pytest==5.4.1

After running the pip freeze command, you will see the list of all python libraries installed on your machine. Now you can double-check if the pygame library is installed.

How to install pygame on mac

Let’s say you check your installed libraries using pip freeze, and you noticed the library list doesn’t include the pygame library. What’s next? You need to install the module using pip. All you need to do is executing the command below:

How to Fix When working from an IDE

Another possible scenario. It could be that you installed the module from the terminal; however, when you execute the game from your IDE, such as PyCharm, you still get the error.

In case you are working on mac, one possible reason is that your IDE is set up to use python 2. Python 2 is installed on the Mac operating system by default.

As a result, if you installed python 3, you have two python installations(python SDKs) in your machine. And your IDE should be set up to use python3, which includes pip as the “module/package manager.” Or the python SDK where the module is installed. Let’s see how to do that.

Modulenotfounderror: no module named 'pygame'

In Pycharm, go to File->Project Structure and check the project SDK. The project SDK indicates the Python installation that pycharm uses to execute your python scripts.

If there is “No SDK” selected, make sure you coose python 3. If python3 is not available in the dropdown, click on “New …” and add the Python SDK( or installation), using the path where you have installed python earlier.

In other words, you need to make your IDE point to the same python SDK, where you have installed the module.

Where is your python SDK?

In case you don’t know or don’t remember where you installed python, no problem. Pretty simple to find out. You can check just by executing the following command from your terminal:

Output:

/Library/Frameworks/Python.framework/Versions/3.7/bin/python3

The above line indicates where python is installed on your machine. The Python SDK will access any module through the pip manager that is part of the SDK. As a result, you should make sure your python command and pip are in the same location/path, which means they are part of the same SDK

Where is pip installed?

You can find out where pip is installed using the which command as well. As I mentioned earlier, make sure python and pip are in the same location, meaning they belong to the same SDK. Check the pip location using the command below:

Output:

/Library/Frameworks/Python.framework/Versions/3.7/bin/pip

The above output means the pip command is installed in the location above, and it is part of the python version 3.7.

What next?

Once you fix the modulenotfounderror, you can start using pygame and writing your own games. You could start by programming a snake game. If that sounds like something interesting, check out the following post where you will find instructions and videos about how to program the snake game using python and pygame.

snake game in python pygame

And you can also check my course about how to program a tetris game with pygame and python:

Conclusion

In conclusion, you will encounter the error Modulenotfounderror: no module named ‘pygame’ for two main reasons. One is that the module is not installed. The other reason is that you installed the module in a different python SDK to the one you are using to executing your game.

I hope you enjoy the article, and thank you so much for reading and supporting this blog. Happy Coding!

More Interesting articles

Modulenotfounderror: no module named 'pygame'
python errors
return statement
dfs in python

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:

  1. 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.
  2. You might have pygame installed in a virtual environment, and you are not activating the virtual environment before running your code.
  3. 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:

  1. Install pygame with pip using /usr/bin/ Python version
  2. Install Python using Homebrew, you have Python in /opt/homebrew/
  3. Then you run import pygame in 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! 👋

Понравилась статья? Поделить с друзьями:
  • Как исправить ошибку mmc exe
  • Как исправить ошибку missing operating system
  • Как исправить ошибку missing map в гаррис моде
  • Как исправить ошибку missing map maps
  • Как исправить ошибку minecraft launcher