Posted by Marta on March 15, 2023 Viewed 43499 times
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!
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:
pip freeze
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.
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:
pip install pygame
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.
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.
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:
which python3
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
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:
which pip
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.
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.
And you can also check my course about how to program a tetris game with pygame and python:
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!
Steady pace book with lots of worked examples. Starting with the basics, and moving to projects, data visualisation, and web applications
Unique lay-out and teaching programming style helping new concepts stick in your memory
Great guide for those who want to improve their skills when writing python code. Easy to understand. Many practical examples
Perfect Boook for anyone who has an alright knowledge of Java and wants to take it to the next level.
Excellent read for anyone who already know how to program and want to learn Best Practices
Perfect book for anyone transitioning into the mid/mid-senior developer level
Great book and probably the best way to practice for interview. Some really good information on how to perform an interview. Code Example in Java