Pip install pygame ошибка

Environment: You can get some of this info from the text that pops up in the console when you run a pygame program. Operating system (e.g. Windows, Linux(Debian), Linux(Ubuntu), Mac): Windows Pytho...

@Matiiss

This is not exactly a bug, it’s that there are no release wheels yet for 3.11, use pip install pygame --pre (--pre stands for pre release), it will install pygame 2.1.3.dev8 and you can use that, 2.1.3 is coming soon but I can’t tell for sure when.

mariolacko, MaxWolf-01, wang551, rammfire, Zageron, aza547, johnp808, jeck3763, mikiweys, hit-the-dust, and 27 more reacted with thumbs up emoji
AntonioGms2007, ghanteyyy, freebird-2, Heebeaux, Be3y4uu-K0T, mikiweys, hit-the-dust, RodionYalyna, theRealProHacker, NikBandi, and 3 more reacted with laugh emoji
unowiz reacted with hooray emoji
marurunk, Px228, puisigblk, LeStefan04, bikrampun, Assentencia, Kocmocy-Da, and des5771 reacted with heart emoji

@ghost

Was able to duplicate this issue with:

  • Python 3.11.0
  • macOS Monterey 12.6
  • Pygame 2.1.2
    I used pip3 install pygame and got a huge error message, as shown in the OP.

I do have a question, if there is no wheel made for Python3 3.11.0, is installing pygame 2.1.3.dev8 the only option? Is this release stable?

@Matiiss

@arknaut Well, that’s the latest version of pygame that has wheels for Python 3.11 so it would be the most stable for that version of Python. Also, yeah, it should be stable enough on its own anyways, but again, it’s kind of temporary, I can’t tell for sure (or at all) but soon wheels for 2.1.3 should be released and they will be made for 3.11 too.

@ghost

@Matiiss — Okay thanks! (just clarifying) pygame 2.1.3.dev8 has been released as a temporary version of pygame so that python 3.11.0 users can continue using pygame.

@Matiiss

Uh, no, it’s more like 2.1.3.dev8 (it’s technically a pre-release btw) happens to have wheels for Python 3.11, it’s not specifically temporarily released for 3.11 because really 2.1.3 is kind of released already, it just doesn’t have wheels yet. But don’t take my word for it, however, I’m pretty sure it’s not something specifically done.

@ghost

@oddbookworm

2.1.3dev8 is the only version with 3.11 wheels prebuilt. If you really want to use 2.1.2, you’ll have to build the wheels yourself. But 2.1.3dev8 is essentially the full 2.1.3 release, but in pre-release form. It wasn’t specifically released to target 3.11, but since 3.11 was coming out soon, compatibility with 3.11 was added to dev8 by starbuck5 and ankith

@fladd

Are there any updates on this? Or an ETA for the new Pygame version?

@MyreMylar

@rammfire

Hello everyone. I observe the same problem on Armbian

pip install pygame
Defaulting to user installation because normal site-packages is not writeable
Collecting pygame
  Downloading pygame-2.1.2.tar.gz (10.1 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 10.1/10.1 MB 3.5 MB/s eta 0:00:00
  Preparing metadata (setup.py) ... error
  error: subprocess-exited-with-error
  
  × python setup.py egg_info did not run successfully.
  │ exit code: 1
  ╰─> [30 lines of output]
      
      
      WARNING, No "Setup" File Exists, Running "buildconfig/config.py"
      Using UNIX configuration...
      
      /bin/sh: 1: sdl2-config: not found
      /bin/sh: 1: sdl2-config: not found
      /bin/sh: 1: sdl2-config: not found
      Traceback (most recent call last):
        File "<string>", line 2, in <module>
        File "<pip-setuptools-caller>", line 34, in <module>
        File "/tmp/pip-install-m1yp62ur/pygame_44d4c02831ed4c49940d26d640b55b6a/setup.py", line 359, in <module>
          buildconfig.config.main(AUTO_CONFIG)
        File "/tmp/pip-install-m1yp62ur/pygame_44d4c02831ed4c49940d26d640b55b6a/buildconfig/config.py", line 225, in main
          deps = CFG.main(**kwds)
        File "/tmp/pip-install-m1yp62ur/pygame_44d4c02831ed4c49940d26d640b55b6a/buildconfig/config_unix.py", line 188, in main
          DependencyProg('SDL', 'SDL_CONFIG', 'sdl2-config', '2.0', ['sdl']),
        File "/tmp/pip-install-m1yp62ur/pygame_44d4c02831ed4c49940d26d640b55b6a/buildconfig/config_unix.py", line 39, in __init__
          self.ver = config[0].strip()
      IndexError: list index out of range
      
      Hunting dependencies...
      
      ---
      For help with compilation see:
          https://www.pygame.org/wiki/Compilation
      To contribute to pygame development see:
          https://www.pygame.org/contribute.html
      ---
      
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.

@oddbookworm

@rammfire if you read a few messages above, Matiiss gave the solution

@fladd

@rammfire if you read a few messages above, Matiiss gave the solution

Unfortunately this is not really a solution for other packages that depend on Pygame.

For instance, I am about to release a new version of Expyriment, which depends on Pygame, and I am currently mainly holding it back due to this issue. Would be great to have some idea on when proper Python 3.11 support is planned (roughly, like within the next few days, or will it still be months?).

@oddbookworm

@fladd The full release of 2.1.3 is almost out. I’m hoping it’ll be within a week or two at most, but that’s really up to the discretion of the lead maintainer right now

@freedom-penguin

Cant wait for the full release ! 😄

PeterJCLaw

added a commit
to PeterJCLaw/python-osc
that referenced
this issue

Nov 17, 2022

@PeterJCLaw

This was referenced

Nov 17, 2022

@liudonghua123

Though pip install pygame --pre works on python 3.11 on my windows 11. I would like to use the release version of pygame. 😄

Looking forward to pygame-2.1.3.

C:UsersLiu.D.H>pip install pygame --pre
Collecting pygame
  Downloading pygame-2.1.3.dev8-cp311-cp311-win_amd64.whl (10.6 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 10.6/10.6 MB 4.8 MB/s eta 0:00:00
Installing collected packages: pygame
Successfully installed pygame-2.1.3.dev8

C:UsersLiu.D.H>

@NikBandi

@AokiAhishatsu

C:UsersLiu.D.H>pip install pygame —pre

Can confirm this works. (Tested on Win10)

@Ivoz

I would advocate for a point release that basically doesn’t change anything from the previous, but simply provides wheels for the new python version. A lot of beginners will get caught up on this while a new patch release is prepared. e.g., 2.1.3 simply ships additional 3.11 wheels, 2.1.4 gets moved to new patch release.

@oddbookworm

I would advocate for a point release that basically doesn’t change anything from the previous, but simply provides wheels for the new python version. A lot of beginners will get caught up on this while a new patch release is prepared. e.g., 2.1.3 simply ships additional 3.11 wheels, 2.1.4 gets moved to new patch release.

To the best of my knowledge, 2.1.3 is ready for release. Just waiting on the head maintainer to actually release it.

@fladd

@fladd The full release of 2.1.3 is almost out. I’m hoping it’ll be within a week or two at most, but that’s really up to the discretion of the lead maintainer right now

Since it has been over a month now: Is there any news regarding the release?

@oddbookworm

Since it has been over a month now: Is there any news regarding the release?

To the best of my knowledge, the lead maintainer has not said anything to anyone in that time. Nobody else has the ability to release 2.1.3, so unfortunately we’re stuck waiting

@oddbookworm

@illume Python 3.11 has been out for a month and a half (and pygame’s main branch has been on 2.1.4dev1 for just as long). More and more people are starting to move to 3.11 and the fact that the only version of pygame with support for 3.11 requires knowledge of how to install a prerelease might detract users from continuing to use pygame. Is there something specific holding back the release? There are also other projects that are waiting for pygame 2.1.3 to fully release so they can update their dependencies.

@ea7kir

I’ve tried and tried, but can’t even instal the pre-release.
RPi 4B, Bullseye (64-bit), Python3.11.

pi@rxtouchlite:~/pgtest $ pip install pygame —pre
Collecting pygame
Downloading pygame-2.1.3.dev8.tar.gz (12.1 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 12.1/12.1 MB 2.8 MB/s eta 0:00:00
Preparing metadata (setup.py) … error
error: subprocess-exited-with-error

× python setup.py egg_info did not run successfully.
│ exit code: 1
╰─> [33 lines of output]

  WARNING, No "Setup" File Exists, Running "buildconfig/config.py"
  Using UNIX configuration...
  
  /bin/sh: 1: sdl2-config: not found
  /bin/sh: 1: sdl2-config: not found
  /bin/sh: 1: sdl2-config: not found
  Traceback (most recent call last):
    File "<string>", line 2, in <module>
    File "<pip-setuptools-caller>", line 34, in <module>
    File "/tmp/pip-install-9_sp3ju8/pygame_06121d1e121e4eafb99504b0ad01a83b/setup.py", line 399, in <module>
      buildconfig.config.main(AUTO_CONFIG)
    File "/tmp/pip-install-9_sp3ju8/pygame_06121d1e121e4eafb99504b0ad01a83b/buildconfig/config.py", line 231, in main
      deps = CFG.main(**kwds, auto_config=auto)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/tmp/pip-install-9_sp3ju8/pygame_06121d1e121e4eafb99504b0ad01a83b/buildconfig/config_unix.py", line 189, in main
      DependencyProg('SDL', 'SDL_CONFIG', 'sdl2-config', '2.0', ['sdl']),
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/tmp/pip-install-9_sp3ju8/pygame_06121d1e121e4eafb99504b0ad01a83b/buildconfig/config_unix.py", line 39, in __init__
      self.ver = config[0].strip()
                 ~~~~~~^^^
  IndexError: list index out of range
  
  Hunting dependencies...
  
  ---
  For help with compilation see:
      https://www.pygame.org/wiki/Compilation
  To contribute to pygame development see:
      https://www.pygame.org/contribute.html
  ---
  
  [end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.

@MyreMylar

Are you sure you are using the pip from python 3.11 there? Pis come with
multiple python versions. I thought you had to use pip3 on there

On Sat, 17 Dec 2022, 11:38 Michael Naylor, ***@***.***> wrote:
I’ve tried and tried, but can’t even instal the pre-release.
RPi 4B, Bullseye (64-bit), Python3.11.

***@***.***:~/pgtest $ pip install pygame —pre
Collecting pygame
Downloading pygame-2.1.3.dev8.tar.gz (12.1 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 12.1/12.1 MB 2.8 MB/s eta 0:00:00
Preparing metadata (setup.py) … error
error: subprocess-exited-with-error

× python setup.py egg_info did not run successfully.
│ exit code: 1
╰─> [33 lines of output]

WARNING, No «Setup» File Exists, Running «buildconfig/config.py»

Using UNIX configuration…

/bin/sh: 1: sdl2-config: not found

/bin/sh: 1: sdl2-config: not found

/bin/sh: 1: sdl2-config: not found

Traceback (most recent call last):

File «<string>», line 2, in <module>

File «<pip-setuptools-caller>», line 34, in <module>

File «/tmp/pip-install-9_sp3ju8/pygame_06121d1e121e4eafb99504b0ad01a83b/setup.py», line 399, in <module>

buildconfig.config.main(AUTO_CONFIG)

File «/tmp/pip-install-9_sp3ju8/pygame_06121d1e121e4eafb99504b0ad01a83b/buildconfig/config.py», line 231, in main

deps = CFG.main(**kwds, auto_config=auto)

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File «/tmp/pip-install-9_sp3ju8/pygame_06121d1e121e4eafb99504b0ad01a83b/buildconfig/config_unix.py», line 189, in main

DependencyProg(‘SDL’, ‘SDL_CONFIG’, ‘sdl2-config’, ‘2.0’, [‘sdl’]),

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File «/tmp/pip-install-9_sp3ju8/pygame_06121d1e121e4eafb99504b0ad01a83b/buildconfig/config_unix.py», line 39, in __init__

self.ver = config[0].strip()

~~~~~~^^^

IndexError: list index out of range

Hunting dependencies…

For help with compilation see:

https://www.pygame.org/wiki/Compilation

To contribute to pygame development see:

https://www.pygame.org/contribute.html

[end of output]

note: This error originates from a subprocess, and is likely not a problem
with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.


Reply to this email directly, view it on GitHub
<#3522 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADGDGGRVNWTT24EY5AYKHATWNWQ4BANCNFSM6AAAAAARQJB5DY>
.
You are receiving this because you commented.Message ID:
***@***.***>

@ea7kir

Are you sure you are using the pip from python 3.11 there? Pis come with multiple python versions. I thought you had to use pip3 on there

I think so. Both ‘pip’ and ‘pip3’ report version 22.3.1 from the same ‘penv global 3.11.0’.

@MyreMylar

Ah, I suspect it is because the pi is looking on the piwheels repository instead of PyPI and it won’t find any 3.11 wheels on piwheels because piwheels only builds wheels for a maximum of python 3.9.

If your pip is configured to use piwheels, but you want to use PyPI instead, you can remove or comment out the extra-index-url configuration in /etc/pip.conf

Otherwise you will have to learn to build pygame from source if you want to use 3.11 on a raspberry PI.

Trinkle23897

added a commit
to Trinkle23897/envpool
that referenced
this issue

Dec 28, 2022

@Trinkle23897

@dlwaters

Had same error. Used temporary fix.

Please, please, please post this solution on the homepage of Pygame!

I’m an experienced coder but new to python and spent several hours thinking I had done something wrong.

@oddbookworm

Unfortunately, the only person with access to the pygame.org website is the same person that can fix this whole mess in the first place. But, they’ve been unresponsive so far.

@dlwaters

But, they’ve been unresponsive so far.

Bummer. First installs always seem to go sideways. :-(

@khalzal

Hello, everybody.

I followed what was written here, but when I entered the «import pygame» command, I got an error (Windows can not find import).

Who can help? Where is my mistake? Which direction in should I look for a solution to the problem?

2023-02-03 161516
2023-02-03 161438

@oddbookworm

Did you try to run import pygame directly in command prompt? You need to be in an actual Python shell to do that (which you can get to just by typing python

@khalzal

Did you try to run import pygame directly in command prompt? You need to be in an actual Python shell to do that (which you can get to just by typing python

Thank you! This little detail helped a lot.

@tayler6000

I’m also trying to install from source at the moment and I’m getting the following error:

urllib.error.HTTPError: HTTP Error 503: Egress is over the account limit.

The pre-release wheel does work for me though.

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.

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 October 4, 2021 Viewed 37245 times

Card image cap

Hey there! 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.

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

Понравилась статья? Поделить с друзьями:
  • Pioneer deh 1500ub amp error
  • Pioneer avh x2600bt error 10
  • Pip install pygame error
  • Pioneer 725 error 23
  • Pip install pycrypto error