Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ModuleNotFoundError: No module named 'pygame.locals'; 'pygame' is not a package

Tags:

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.

like image 676
Polen Polen Avatar asked Nov 24 '17 18:11

Polen Polen


People also ask

How do I fix No module named pygame?

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.

How do I import pygame into python 3?

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'.

What is pygame locals in python?

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.


1 Answers

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.

like image 67
Rohan Avatar answered Sep 22 '22 12:09

Rohan