thumbnail for module not found error

How To Fix The Module Not Found Error In Pygame

Introduction

You’ve installed python, opened up your editor and typed up some code. Then you run it and are stopped by this error: “ModuleNotFoundError: No module named ‘pygame'”.

module not found error

There are a few common causes for the module not found error, and in this article I will go through each of these one by one to find the fix that works.

What does Module Not Found mean?

If you are using a library that isn’t part of the python code, such as pygame in this example, then you would begin your code by importing that library with a line that says “import pygame”. When you run the code, python will read that line and it will go looking for this library to try to import it. Libraries are all stored together inside a common folder so when your code attempts to import a library, it will look in that folder and if it can’t find it there, it will stop and return this error to let you know that it tried looking but it couldn’t find the library.
 
But what if you’re sure you have installed the library? Well that’s what makes the error annoying to fix. We will need to go through a check list of the common issues to see what is causing it.

How To Fix the Module Not Found Error?

This will follow a few simple steps to figure out what is causing the problem so it can be fixed:

  • Step 1: Check your python and PIP Installations
  • Step 2: Check that the library (pygame) is installed correctly
  • Step 3: Check and configure the interpreter used in your editor (VS Code & Pycharm for example)

Step 1 - Check your Python and PIP installs

Starting at the beginning, the first thing to do is check that your python and pip are correctly installed. Open up your command prompt by going into the start menu and typing CMD. Once there, type python and hit enter. You should see some information on the install to confirm that python is correctly installed. Type exit() to come out of the python environment.
python install check

Now type pip and hit enter. You should get a bunch of data displayed again.

pip install check

If either of these give you an error then you need to install python first.

Go to http://python.org and download and install python.

IMPORTANT: When installing Python, make sure to click the checkbox “Add python.exe to PATH”

Step 2 - Check Your Library is Installed

Sticking with the command prompt, you can now check what libraries are installed on your computer. Type pip list in your command prompt and it will give you a list of all the libraries that are installed. Look through this list and see if the library you are trying to install is in that list. In this example we are looking for pygame.

This is the output on my computer. You can see the libraries that I have installed, including pygame.

If you don’t see the library in this list, then this is likely the cause of the problem and you need to install it.

To do this, type pip install pygame into the command prompt, which will download and install the library. You can replace pygame with any other python library.

Step 3 - Check Your Editor

If all the above is done and you are still getting this module not found error, then the cause may be in your editor.

Some editors will create their own local installation of python or their own virtual environment, so when you install a library using pip , the editor doesn’t see it because it is looking inside it’s own python folders.

The fix depends on your editor so I’ve detailed the two common ones:

  • Visual Studio Code (VS Code)
  • Pycharm

VS Code

Click on the python interpreter version in the bottom right of VS code, which will bring up the option of selecting a different interpreter.

VS code bottom right interpreter

Try selecting a different interpreter from the list.

Pycharm

Similarly, in Pycharm, click on the name of the current python interpreter in the bottom right and select “Interpreter Settings”

Here you can change the interpreter from the drop down menu.

Pycharm will also list out all of the libraries or packages that are installed with that interpreter. You can look through the list to see if the module you require is there and if it isn’t you can install it directly through pycharm by clicking on the “plus” icon.

Search for pygame (or any other python library) then hit “Install package”

Conclusion

An that’s it. You should now be able to run your code without the dreaded module not found error.

2 thoughts on “How To Fix The Module Not Found Error In Pygame

Leave a Reply

Your email address will not be published. Required fields are marked *