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'”.
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?
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
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. Now type pip
and hit enter. You should get a bunch of data displayed again.
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.
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.
Changing the interpreter in VSC solved the issue. Thanks
Glad you got it working!
Thanks a lot! I was facing problem is VS code and its good that you have detailed each step and for every editor. Finally the module problem is fxed.
Glad to hear it was helpful!