Importing module provides SyntaxError
Question: I have a module using Pygame and sys:

    
    
    import pygame, sys
    
    font = pygame.font.SysFont('sans-serif', 72)
    
    class console:
    
        def text(self, surface, text, pos):
            text = font.render(text, 1, (255, 255, 255))
            surface.blit(text, (pos[0], pos[1]))
    

This module returns no errors of any kind. I also have a short main program
designed to import and use the module:

    
    
    import pygame
    pygame.init()
    import pygame-console.py
    
    screen = pygame.display.set_mode([640, 480])
    
    console = pygame-console.console()
    
    console.text(screen, 'Hello World!', (0, 0))
    
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
    

The main program returns a SyntaxError on the third line, like so:

    
    
    File "C:\Python25\pgTESTimport.py" line 3
    import pygame-console.py
    ^
    SyntaxError: invalid syntax
    Script terminated.
    

I'm using Stani's Python Editor due to IDLE's difficulty with sys.exit(). Can
anybody tell me what the problem is with this code? Why does it return
SyntaxError on normal-looking code?


Answer: That is not how you import modules, you do not need to give the `.py` at the
end, also you cannot directly import modules with `-` in the name.

You would either need to rename your file to something without a `-` in it,
like `pygameconsole.py` and then import it using -

    
    
    import pygameconsole
    

Or, if you are do not want to rename the file, and want to keep the `-` in the
name, you can use `importlib` to import it -

    
    
    import importlib
    pygameconsole = importlib.import_module('pygame-console')
    

Then you can use it using `pygameconsole` .

