Ошибка no module named pygame

I have installed python 3.3.2 and pygame 1.9.2a0. Whenever I try to import pygame by typing:

import pygame  

I get following error message :

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import pygame
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import pygame
ImportError: No module named 'pygame'
>>> 

I went through some of the questions related to this error but none of the solution helped.
I have 64 bit machine with Win7 OS.

user_na's user avatar

user_na

2,1431 gold badge15 silver badges36 bronze badges

asked Aug 19, 2013 at 15:26

user2398618's user avatar

5

go to python/scripts folder, open a command window to this path, type the
following:

C:python34scripts> python -m pip install pygame

To test it, open python IDE and type

import pygame

print (pygame.ver)

It worked for me…

LoukasPap's user avatar

answered May 4, 2017 at 9:33

Srikar Madhavapeddy's user avatar

3

Here are instructions for users with the newer Python 3.5 (Google brought me here, I suspect other 3.5 users might end up here as well):

I just successfully installed Pygame 1.9.2a0-cp35 on Windows and it runs with Python 3.5.1.

  • Install Python, and remember the install location
  • Go here and download pygame-1.9.2a0-cp35-none-win32.whl
  • Move the downloaded .whl file to your python35/Scripts directory
  • Open a command prompt in the Scripts directory (ShiftRight click in the directory > Open a command window here)
  • Enter the command:

    pip3 install pygame-1.9.2a0-cp35-none-win32.whl

  • If you get an error in the last step, try:

    python -m pip install pygame-1.9.2a0-cp35-none-win32.whl

And that should do it. Tested as working on Windows 10 64bit.

answered Apr 11, 2016 at 16:02

charlie's user avatar

charliecharlie

8561 gold badge11 silver badges17 bronze badges

4

I was trying to figure this out for at least an hour. And you’re right the problem is that the installation files are all for 32 bit.

Luckily I found a link to the 64 pygame download! Here it is: http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame

Just pick the corresponding version according to your python version and it should work like magic. The installation feature will bring you to a bright-blue screen as the installation (at this point you know that the installation is correct for you.

Then go into the Python IDLE and type «import pygame» and you should not get any more errors.

Props go to @yuvi who shared the link with StackOverflow.

answered May 1, 2014 at 23:27

aaron-coding's user avatar

aaron-codingaaron-coding

2,5611 gold badge23 silver badges31 bronze badges

5

  1. open the folder where your python is installed
  2. open scripts folder
  3. type cmd in the address bar. It opens a command prompt window in that location
  4. type pip install pygame and press enter
  5. it should download and install pygame module
  6. now run your code. It works fine :-)

answered Dec 28, 2017 at 5:42

NARENDER REDDY's user avatar

I had the same problem and discovered that Pygame doesn’t work for Python3 at least on the Mac OS, but I also have Tython2 installed in my computer as you probably do too, so when I use Pygame, I switch the path so that it uses python2 instead of python3. I use Sublime Text as my text editor so I just go to
Tools > Build Systems > New Build System and enter the following:

{
    "cmd": ["/usr/local/bin/python", "-u", "$file"],    
}

instead of

{
    "cmd": ["/usr/local/bin/python3", "-u", "$file"],   
}

in my case. And when I’m not using pygame, I simply change the path back so that I can use Python3.

General Failure's user avatar

answered Dec 26, 2016 at 6:34

Megan Chang's user avatar

The current PyGame release, 1.9.6 doesn’t support Python 3.9. I fyou don’t want to wait for PyGame 2.0, you have to use Python 3.8. Alternatively, you can install a developer version by explicitly specifying the version (2.0.0.dev20 is the latest release at the time of writing):

pip install pygame==2.0.0.dev20

or try to install a pre-release version by enabling the --pre option:

pip install pygame --pre

answered Oct 23, 2020 at 5:51

Rabbid76's user avatar

Rabbid76Rabbid76

200k26 gold badges129 silver badges171 bronze badges

Resolved !

Here is an example

C:UsersuserAppDataLocalProgramsPythonPython36-32Scripts>pip install pygame

John's user avatar

John

2,5934 gold badges19 silver badges33 bronze badges

answered Nov 29, 2019 at 16:43

diego lara's user avatar

try this in your command prompt:
python -m pip install pygame

Oscar Nguyen's user avatar

answered Feb 18, 2021 at 17:21

Ace Standard's user avatar

I was getting the same error. It is because your version of Pygame is not compatible with your version of Python or Pydev. Go to this link and get the proper version of Pygame for your current version of Python. Ctrl F to find it faster or click on the word python in blue. up at the top. While you instal Pygame it should find the Python path by itself. At least mind did any ways. I run Pygame through Eclipse with Python 3.4.

http://www.lfd.uci.edu/~gohlke/pythonlibs/

answered Nov 14, 2014 at 2:20

TriGeo's user avatar

TriGeoTriGeo

351 silver badge6 bronze badges

1

Since no answer stated this:

Make sure that, if you are using a virtual environment, you have activated it before trying to run the program.

If you don’t really know if you are using a virtual environment or not, check with the other contributors of the project. Or maybe try to find a file with the name activate like this: find . -name activate.

answered May 31, 2017 at 8:08

user2089810's user avatar

  1. Install and download pygame .whl file.
  2. Move .whl file to your python35/Scripts
  3. Go to cmd
  4. Change directory to python scripts
  5. Type:

    pip install pygame
    

Here is an example:

C:UsersuserAppDataLocalProgramsPythonPython36-32Scripts>pip install pygame

Stephen Rauch's user avatar

Stephen Rauch

47.5k31 gold badges106 silver badges135 bronze badges

answered Dec 14, 2017 at 3:35

EDGE 074's user avatar

I just encountered the same problem and found that I am having multiple interpreters of the different versions installed in my system and pygame got installed in one of them when I installed it using command but in my IDE another interpreter was selected so this messed up my system, try to see if you are also having the same situation.

answered Jun 26, 2021 at 15:47

Tanishq chandra's user avatar

Just use this command in the terminal python3 -m pip install -U pygame --user

answered Sep 22, 2022 at 14:51

Eduardo-Puentes's user avatar

You don’t need 64 bit Python on Win64 system, just install the 32bit versions of both Python and Pygame and they will work just fine (and there is a ton more modules for them anyways).

answered Aug 18, 2014 at 21:58

KalELonRedKryptonite's user avatar

3

I’m using the PyCharm IDE. I could get Pygame to work with IDLE but not with PyCharm. This video helped me install Pygame through PyCharm.

(It seems that PyCharm only recognizes a package; if you use its GUI.)

However, there were a few slight differences for me; because I’m using Windows instead of a Mac.

My “preferences” menu is found in: File->Settings…

Then, in the next screen, I expanded my project menu, and clicked Project Interpreter. Then I clicked the green plus icon to the right to get to the Available Packages screen.

answered Mar 6, 2018 at 12:34

Joe's user avatar

JoeJoe

8,1313 gold badges17 silver badges23 bronze badges

I ran into the 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 copies 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!!!

answered Aug 28, 2018 at 21:39

First execute python3 then type the command import pygame,now you can see the output

answered Apr 16, 2020 at 11:06

Asha's user avatar

AshaAsha

7873 gold badges9 silver badges17 bronze badges

For this you have to install pygame package from the cmd (on Windows) or from terminal (on mac). Just type pip install pygame
.If it doesn’t work for you, then try using this statement pip3 install pygame .
If it is still showing an error then you don’t have pip installed on your device and try installing pip first.

answered Aug 21, 2020 at 20:04

Ansh's user avatar

AnshAnsh

586 bronze badges

make sure if you are on windows that your library directory is added to path

answered Aug 14, 2021 at 9:21

dragon445's user avatar

dragon445dragon445

1211 silver badge4 bronze badges

This may happen when pygame didn’t installed, install the pygame first

pip
pip install pygame

if dont work update the PIP by goto python install folder and type

python -m pip install --upgrade pip

hope it work

answered Aug 26, 2021 at 12:24

Azka Hamidzan F.'s user avatar

Try this solution:
Type in to cmd (Windows):

C:Users'Your name'> pip install -U pygame

You should remove python -m, py -m, python3 -m before the pip
Also remove --user behind it.

It will said:

C:Usersviait>pip install -U pygame
Defaulting to user installation because normal site-packages is not writeable
Collecting pygame
  Downloading pygame-2.1.2-cp310-cp310-win_amd64.whl (8.4 MB)
     ---------------------------------------- 8.4/8.4 MB 1.7 MB/s eta 0:00:00
Installing collected packages: pygame
Successfully installed pygame-2.1.2

Then test it in your IDE or cmd:
(CMD example)

C:Usersviait>python
Python 3.10.3 (tags/v3.10.3:a342a49, Mar 16 2022, 13:07:40) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
pygame 2.1.2 (SDL 2.0.18, Python 3.10.3)
Hello from the pygame community. https://www.pygame.org/contribute.html

(IDE example)

import pygame

You can do this without any errors.

answered Apr 5, 2022 at 9:17

Oscar Nguyen's user avatar

You could use

pip install pygame

but if you use IDE like PyCharm, then you could just either install it from Python Packages or use right click at the package name then left click on Show Context Actions then left click on Install package pygame

(Personally, I recommended using Python Packages for the package installing because it has documentation with it)

answered Nov 26, 2022 at 10:42

Foryxled_Dev's user avatar

You gotta use Pycharm and install it in Terminal using pip install pygame and also after that enter Pycharm and hover on pygame in the «Import pygame» and in Pycharm it will tell you to download that and you can easily download it and enjoy your result

answered Jun 4, 2021 at 14:13

Mushfiqur Rahman's user avatar

I was having the same trouble and I did

pip install pygame

and that worked for me!

answered Nov 30, 2016 at 18:38

TheHyperCake's user avatar

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 40926 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

Win10, Spyder5, Anaconda3.

import pygame as pg приводит к сообщению об ошибке ModuleNotFoundError: No module named ‘pygame’

Модуль был загружен (pip install pygame)
В списке pip list (Anaconda) этот модуль присутствует.

Других версий Python на компьютере нет.


  • Вопрос задан

    более года назад

  • 629 просмотров

Пригласить эксперта

перейдите в папку python/scripts, откройте командное окно к этому пути, введите следующее:

C:python34scripts> python -m pip install pygame
Чтобы проверить это, откройте python IDE и введите

import pygame

print (pygame.ver)
Это сработало для меня…


  • Показать ещё
    Загружается…

07 июн. 2023, в 10:19

20000 руб./за проект

07 июн. 2023, в 10:07

500 руб./за проект

07 июн. 2023, в 08:55

500 руб./за проект

Минуточку внимания

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! 👋

Pygame is a cross-platform Python module that allows you to develop video games. It has inbuilt computer graphics and audios that make easy development of the games. To use it you have to install it on your system. Many programmers are not able to install pygame module in their system and get the no module named pygame error.

In this entire tutorial, you will know how to solve the ModuleNotFound: no module named ‘pygame’ error.

Why do you get the ModulNotFound Error?

The modulenotfound error generally comes when you had not properly installed that python package in your system. Most of the cases are solved by just installing the package in the system.

Root Cause of no module named ‘pygame’ error

The main or root cause for getting this modulenotfound error is that you have not installed pygame in your system. And another reason is your python interpreter has not known the path of the package installed.

You will get the red underline below the package name if you try to import pygame package in pycharm.

import pygame

Output

No module named pygame error

pygame modulenotfound error

Solution of ModuleNotFoundErro: no module named ‘pygame’ error

The solution for this no module named pygame error is very simple. You have to install it in the system using the pip command.

But before that, you have to also check the version of the python. To check it open your command prompt or terminal and write the below command.

python --version

Checking the Python version

Checking the Python version

The prompt or terminal will show the python version. If it is python 3. xx then use the pip3 command and if it is python 2. xx then use the pip command.

I have already checked the version of python for my system and it is python 3. xx. Therefore I will use the pip3 command.

Run the following bash command to install the pygame package.

pip3 install pygame

Installing pygame in the system

Installing pygame in the system

After the installation, if you again import the package then you will not see the red underline and also no module named pygame error in pycharm.

Checking whether pygame is installed or not

Checking whether pygame is installed or not

Conclusion

Pygame allows you to develop video games using the python language. There are also many other programming languages to develop video games.  However, if you know knowledge of python is above good then go for it.

The above method will solve the error if you are getting no module named pygame.

Modulenotfounderror: no module named Pygame error occurs when the module Pygame is not installed properly before invoking into the code. Sometimes we install the Pygame module but either the package manager ( pip, conda, etc ) is misconfigured or Pygame has permission related issues while installation. In such scenarios, we encounter this modulenotfounderror. Well, The best and easy way to fix this error is to reinstall the package. In this article, we will explore multiple ways to install or reinstall pygame module.

Modulenotfounderror: no module named Pygame ( Solution Approaches) –

We can use a package manager or source code to install the package.  Well, using a package manager like pip, etc is much easier than going with the source code itself. Let’s start with pip.

Approach 1: Pygame  Installation with pip package manager –

If you want to install the latest version of Pygame then use the below code.

pip install pygame

The best part with pip pygame installation is it provides multiple options to control versions, permission access, etc in very minimal change in installation command. Let’s explore them.

Modulenotfounderror No module named pygame

Modulenotfounderror No module named pygame

1. Suppose you do not want to install the latest version of pygame because of some existing incompatibility then you can provide the version along with the command. Here is the way for this.

pip install pygame==2.1.1

2. Providing admin rights with commands is simple—all we need to add sudo keyword with command.

sudo pip install pygame==2.1.1

Approach 2: Pygame  Installation with conda package manager –

Conda is simple just like the pip package manager for Pygame installation. But My personal preference is pip over conda. Because it gives multiple option for alternative configurations.

conda install -c cogsci pygame

pygame installation with conda

pygame installation with conda

Approach 3: Pygame  Installation with Source code –

If you want to install the Pygame module from the source code then we need to first download it. Then we need to run the below command.

python setup.py install

pygame installation with source code

pygame installation with source code

Thanks 

Data Science Learner Team

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

We respect your privacy and take protecting it seriously

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

I forget to install the pygame module thats why we face ModuleNotFoundError: No module named ‘pygame’ error in python, pygame is not pre-install module we need to install separately see below command how we can install pygame pip in our operating system.

Error Massage

import pygame
Traceback (most recent call last):
  File "/home/kali/python/webproject/Py_Games/pygame/main.py", line 1, in <module>
    import pygame
ModuleNotFoundError: No module named 'pygame'

How to install pygame in windows

Firstly you need to open command prompt window so please press Win+X. When the context menu appears, press A. then just copy paste below command.

py -m pip install -U pygame --user

After successfully install then just confirm we below command.

py -m pygame.examples.aliens

How to install pygame in Linux Terminal

Firstly open your Linux Terminal then run below command.

sudo apt-get install python3-pygame

For Linux after successfully install then just confirm we below command.

python3 -m pygame.examples.aliens

How to install pygame in Mac

Firstly open your Mac Terminal then run below command.

python3 -m pip install -U pygame --user

Know we have pygame then use it and try to make beautiful games but if you looking pygame game source code please visited Amol Blog Python Games.

This article will tell you how to install the Python Pygame module correctly and how to fix the problems during the installation process.

1. Install Python Pygame Module Use The PIP Install Command.

  1. Open the terminal and run the command pip install pygame in it.
    $ pip install pygame
    Collecting pygame
      Downloading pygame-2.1.2-cp37-cp37m-macosx_10_9_x86_64.whl (8.9 MB)
         |████████████████████████████████| 8.9 MB 1.2 MB/s 
    Installing collected packages: pygame
    Successfully installed pygame-2.1.2
    
  2. Run the command pip show pygame to verify the module installation.
    $ pip show pygame
    Name: pygame
    Version: 2.1.2
    Summary: Python Game Development
    Home-page: https://www.pygame.org
    Author: A community project.
    Author-email: [email protected]
    License: LGPL
    Location: /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages
    Requires: 
    Required-by:

2. Install Python Pygame Module Use The Binary Installer File.

  1. Besides using the pip install command, we can also install it using the downloaded binary installer file.
  2. Go to the Pygame GitHub website to download the Pygame installer file that matches your OS and Python versions.
  3. Open a terminal and go to the Pygame installer downloaded directory.
  4. Run the command python -m pip install –user pygame-2.1.2-cp37-cp37m-macosx_10_9_x86_64.whl to install the Pygame from the installer file.
    $ python -m pip install --user pygame-2.1.2-cp37-cp37m-macosx_10_9_x86_64.whl
    Processing ./pygame-2.1.2-cp37-cp37m-macosx_10_9_x86_64.whl
    Installing collected packages: pygame
    Successfully installed pygame-2.1.2
    
  5. Run the command python -m pygame –version to verify the installed Pygame moduel version.
    $ python -m pygame --version
    pygame 2.1.2 (SDL 2.0.18, Python 3.7.3)
    Hello from the pygame community. https://www.pygame.org/contribute.html
    /Library/Frameworks/Python.framework/Versions/3.7/bin/python: No module named pygame.__main__; 'pygame' is a package and cannot be directly executed

3. How To Fix The ImportError: No module named ‘pygame’.

3.1 Question.

  1. I installed pygame 1.9.2 in Windows, and my python version is 3.5. But when I import the pygame module in the python source code, it shows the error ImportError: No module named ‘pygame’. How can I fix this error?
    >>> import pygame
    Traceback (most recent call last):
    File "<pyshell#0>", line 1, in <module>
    import pygame
    ImportError: No module named 'pygame'

3.2 Answer1.

  1. Below are the steps to install Pygame 1.9.2 on Python 3.5.1 for Windows, you can try it.
  2. After you successfully install Python 3.5, you should write down the Python 3.5 installed folder for later use.
  3. Download the wheel file pygame-1.9.2a0-cp35-none-win32.whl and save it to the folder python35/Scripts.
  4. Open a dos window and cd to the python35/Scripts folder, then run the below command.
    pip3 install pygame-1.9.2a0-cp35-none-win32.whl
  5. If you can not run the above command successfully, you can run the below command instead.
    python -m pip install pygame-1.9.2a0-cp35-none-win32.whl
  6. Now when you import the pygame module, the error should disappear.

3.3 Answer2.

  1. I also meet such an error, but the reason is different.
  2. I installed multiple Python versions on my Windows, and I installed pygame library successfully into one of my Python versions.
  3. But my IDE used python interpreter does not contain the pygame library, so when I import pygame in my IDE the error is shown.
  4. After I select the python interpreter that has installed pygame library, the error disappear.
  5. wish this can help you too.

3.4 Answer3.

  1. This error happened when you do not install pygame successfully.
  2. Or you may have multiple Python versions installed, and pygame is installed in one of the python versions, but you import pygame in another python version.
  3. So you should make sure the pygame library has been installed in your python env, you can run the below command to check it.
    (MyPythonEnv) C:Userszhaosong>pip show pygame
    Name: pygame
    Version: 2.1.2
    Summary: Python Game Development
    Home-page: https://www.pygame.org
    Author: A community project.
    Author-email: [email protected]
    License: LGPL
    Location: c:userszhaosonganaconda3envsmypythonenvlibsite-packages
    Requires:
    Required-by:

4. How To Fix ERROR: Command errored out with exit status 1:…EOFError: EOF when reading a line When Install Pygame Using PIP.

4.1 Question.

  1. I run the command pip install pygame on windows 10, but it throws the error with the message ERROR: Command errored out with exit status 1:…EOFError: EOF when reading a line. My python version is 3.9. Below is the detailed error message. How can I fix it?
    Collecting pygame
      Using cached pygame-1.9.6.tar.gz (3.2 MB)
        ERROR: Command errored out with exit status 1:
         command: 'c:usersjerryappdatalocalprogramspythonpython39python.exe' -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\Users\jerry\AppData\Local\Temp\pip-install-4b04m745\pygame\setup.py'"'"'; __file__='"'"'C:\Users\jerry\AppData\Local\Temp\pip-install-4b04m745\pygame\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'rn'"'"', '"'"'n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base 'C:UsersjerryAppDataLocalTemppip-pip-egg-info-up4741kz'
             cwd: C:UsersjerryAppDataLocalTemppip-install-4b04m745pygame
        Complete output (17 lines):
    
    
        WARNING, No "Setup" File Exists, Running "buildconfig/config.py"
        Using WINDOWS configuration...
    
    
        Download prebuilts to "prebuilt_downloads" and copy to "./prebuilt-x64"? [Y/n]Traceback (most recent call last):
          File "<string>", line 1, in <module>
          File "C:UsersjerryAppDataLocalTemppip-install-4b04m745pygamesetup.py", line 194, in <module>
            buildconfig.config.main(AUTO_CONFIG)
          File "C:UsersjerryAppDataLocalTemppip-install-4b04m745pygamebuildconfigconfig.py", line 210, in main
            deps = CFG.main(**kwds)
          File "C:UsersjerryAppDataLocalTemppip-install-4b04m745pygamebuildconfigconfig_win.py", line 576, in main
            and download_win_prebuilt.ask(**download_kwargs):
          File "C:UsersjerryAppDataLocalTemppip-install-4b04m745pygamebuildconfigdownload_win_prebuilt.py", line 302, in ask
            reply = raw_input(
        EOFError: EOF when reading a line
        ----------------------------------------
    ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

4.2 Answer1.

  1. From the message you provide, we can see that you just install the Pygame version 1.9.6. And your python version is 3.9.
  2. But Pygame 1.9.6 does not support python 3.9 now. So you had better use python 3.8 instead.
  3. Or you can install Pygame 2.0 with the command pip install pygame==2.0.0, Pygame 2.0 support python 3.9.
  4. You can also download the Pygame wheel file from https://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame, it contains the wheel file pygame‑1.9.6‑cp39‑cp39‑win32.whl that meet your needs.

Понравилась статья? Поделить с друзьями:
  • Ошибка no module named pandas
  • Ошибка no matching function for call to
  • Ошибка no main manifest attribute in jar
  • Ошибка no jvm could be found
  • Ошибка no function definition vlax get acad object