So this problem is a bit strange for me. I wrote this piece of code to see if pygame works correctly.
import pygame,sys
from pygame.locals import *
pygame.init()
DISPLAYSURF = pygame.display.set_mode((400,300))
pygame.display.set_caption("Hello World")
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
I saved this file as pygame.py
and when I typed:
-python pygame.py
on the cmd it says:
ModuleNotFoundError: No module named 'pygame.locals'; 'pygame' is not a package
And if I type -python
to the shell and then type import pygame
it works like a charm.
So In summary: If I want to execute pygame.py
, it does not see the module,
but it sees the module after typing python
and import pygame
(works without error).
The operating system is Windows.
The Python "ModuleNotFoundError: No module named 'pygame'" occurs when we forget to install the pygame module before importing it or install it in an incorrect environment. To solve the error, install the module by running the pip install pygame command.
Open a terminal, and type 'sudo apt-get install idle pygame', enter your password and type 'y' at the prompts, if necessary. 2. After the installation completes, enter 'python' in the terminal to launch Python. Verify that it's using version 2.7 or newer, then at the Python prompt enter 'import pygame'.
This module contains various constants used by pygame. Its contents are automatically placed in the pygame module namespace. However, an application can use pygame. locals to include only the pygame constants with a from pygame.
Naming the file pygame.py makes the computer think of the file when importing, not the package. That is why it says pygame is not a package because pygame is the file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With