Polen Polen
Polen Polen

Reputation: 53

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

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.

Upvotes: 5

Views: 6883

Answers (1)

Rohan
Rohan

Reputation: 563

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.

Upvotes: 7

Related Questions