JiaweiK
JiaweiK

Reputation: 103

AttributeError: 'module' object has no attribute 'locals'

I am trying to go through the book "Making Games with Python & Pygame" and I got this error message while I tried the first blankgame.py example. When I used

from pygame.locals import *

I got NO error with

if event.type == QUIT:

However. if I tried

if event.type == pygame.locals.QUIT

without importing at the beginning and excuted, the error occured. Could anybody help me with this and tell me the difference between importing before hand and using the full constant path reference of QUIT.

Thank you.

Upvotes: 2

Views: 3374

Answers (1)

user3885927
user3885927

Reputation: 3503

from pygame.locals import *

That will import everything from 'pygame.locals' into your local namespace. So you would access the members of that module as if they are in your own current namespace. Hence you should not prefix calls with pygame.locals. If you call with a prefix like pygame.locals.QUIT it will be an error.

import pygame.locals

This will load everything from 'pygame.locals' but not into your namespace. You still have to prefix calls with 'pygame.locals' like pygame.locals.QUIT.

Using pygame.locals.QUIT without using the above import will cause you an error since it never loaded the module pygame.locals

Upvotes: 2

Related Questions