Modulenotfounderror 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,1641 gold badge16 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:\python34\scripts> 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

LoukasPap

1,2541 gold badge8 silver badges17 bronze badges

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,5811 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

203k27 gold badges134 silver badges178 bronze badges

Resolved !

Here is an example

C:\Users\user\AppData\Local\Programs\Python\Python36-32\Scripts>pip install pygame

John's user avatar

John

2,6334 gold badges19 silver badges34 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:\Users\user\AppData\Local\Programs\Python\Python36-32\Scripts>pip install pygame

Stephen Rauch's user avatar

Stephen Rauch

47.9k31 gold badges107 silver badges136 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,2713 gold badges18 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

8153 gold badges10 silver badges18 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

566 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:\Users\viait>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:\Users\viait>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

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:\Users\aditya dand\AppData\Local\Programs\Python\Python36\lib\site-packages\pygame__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?

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

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!

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.

Понравилась статья? Поделить с друзьями:
  • Module time has no attribute clock ошибка
  • Module object is not callable python ошибка
  • Module datetime has no attribute now ошибка
  • Modr клуб романтики ошибка
  • Modorganizer exe системная ошибка