import * only allowed at module level. Python 3.3 using Pyscripter

user3189015 picture user3189015 · Jan 13, 2014 · Viewed 8k times · Source

I'm fairly new to programming in general and I just started using python to try and make a simple game using pygame. If I run the following code in the Python IDLE shell it works fine but if I use Pyscripter I get the error:

SyntaxError: import * only allowed at module level

I really like using Pyscripter because so far it has made learning the syntax much easier but now I don't understand what is wrong. Any help would be great. Thanks.

import pygame, sys
from pygame.locals import *
pygame.init()
DISPLAYSURF = pygame.display.set_mode((400,300),0,32)
pygame.display.set_caption('Hello World!')
while True: #main game loop
     for event in pygame.event.get():
         if event.type == QUIT:
            pygame.quit()
            sys.exit()
     pygame.display.update()

Answer

Krishnan Shankar picture Krishnan Shankar · Apr 28, 2020

The problem is when you execute from pygame.locals import * you are accessing all from a file, not a module. When you do something like from pygame import *, it should work. It is just that you can only use import * at the module level