0

I am trying to import a python script (graphics.py) into my other script (game.py), however, it's not working.

In game.py:

import pygame
import graphics.py
import sys # Mainly for sys.quit

"""********Start variables********"""

# Some colour constants
BLACK = (  0,   0,   0)
WHITE = (255, 255, 255)
RED   = (255,   0,   0)
GREEN = (  0, 255,   0)
BLUE  = (  0,   0, 255)

# Display settings
FPS = 60
fpsClock = pygame.time.Clock()


def main():
    pygame.init()

    # Load graphics
    gfx = graphics()

And in graphics.py:

import pygame

class graphics(object):

    # Create a surface that will hold the player sprite
    SprPlayer = pygame.Surface((32, 32))

    # At the beggining of the object, load the sprites
    def __init__():
        SprPlayer = pygame.image.load("Graphics\Player.png")

So I'm making my first python game, (Python is my new favorite language other than GML.) and I can't have everything in one file. Here is the error

import graphics.py
ImportError: No module named py

I have both scripts in the same directory, so I don't know what's going on. Any help is greatly appreciated.

null
  • 548
  • 2
  • 6
  • 17

2 Answers2

1

In Python you don't need to include the file extension when you are trying to import python modules, hence the No module named py because python is trying to import a file called py from a folder called graphics. What you want is just import graphics.

https://docs.python.org/2/tutorial/modules.html

vishen
  • 459
  • 2
  • 7
1

In python, you do not need to add the '.py' extension on the end of a file from the same directory. Try import graphics. To learn more, take a look at this stackoverflow answer.

Community
  • 1
  • 1
Will
  • 36
  • 3