0

I'm having a problem trying to import another python script correctly using pygame.

This is my file structure:

folder 1
    a1.py
    folder 2
        a2.py
        img1.py
        folder 3
            img1.bmp

a1.py

import os, sys
import pygame

sys.path.insert(0, os.path.join('E:\Raspberry 
Engine\projects\TestGame\Scripts'))

from tiger import *
from ASTRO2 import *

(ScrnWidth, ScrnHeight) = (1080, 720)
screen = pygame.display.set_mode((ScrnWidth, ScrnHeight))

pygame.display.set_caption("Raspberry Engine")

backgroundcolour = (255,255,255)
screen.fill(backgroundcolour)

Running = True
while Running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            Running = False
            pygame.quit()

    screen.blit(tiger,(tigerX,TigerY))

    pygame.display.flip()

When running a1.py, it is opening img1.py (same as a2.py) but for whatever reason, img1.py is only opening the image for a2.py and not for a1.py (img1.py and error are displayed later).

a2.py

import pygame

from tiger import *
from ASTRO2 import *

(width, height) = (1080, 720)
screen = pygame.display.set_mode((width, height))

pygame.display.set_caption("Test Game")

backgroundcolour = (255,255,255)
screen.fill(backgroundcolour)

Running = True
while Running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            Running = False
            pygame.quit()

    screen.blit(tiger,(tigerX,tigerY))
    screen.blit(ASTRO2,(ASTRO2X,ASTRO2Y))

    pygame.display.flip()

img1.py

import pygame
import os, sys

tiger = pygame.image.load(os.path.join('Images','tiger.bmp'))
tigerX = 10
tigerY = 10

img1.py is supposed to open tiger.bmp (img1.bmp), it works correctly in a2.py but for some reason when trying to do it with a1.py it doesn't work and get the following error:

Traceback (most recent call last):
  File "F:\Raspberry Engine\Engine.py", line 6, in <module>
    from tiger import *
ModuleNotFoundError: No module named 'tiger'

Could anyone help? I need a1.py to do the same thing as a2.py from a different directory.

Micheal O'Dwyer
  • 1,237
  • 1
  • 16
  • 26
Benzaboy
  • 1
  • 1
  • Why do you need `img1.py` to load the file? Why aren't you just loading the files in each respective file where you need them? – Micheal O'Dwyer Jan 17 '18 at 16:48
  • Because its more organised – Benzaboy Jan 17 '18 at 17:44
  • and it means its easier to share data to a1.py and a2.py – Benzaboy Jan 17 '18 at 20:06
  • why do you add path to `sys.path` in `a1.py` but not in `a2.py` ? – furas Jan 17 '18 at 21:02
  • BTW: you use `folder 1` in structure but you don't use `folder 1` in code - better create example with real names. – furas Jan 17 '18 at 21:04
  • folder 1 contains the a1.py script, make it easier, im making a 2d game engine, a1.py is the engine script, a2.py is the game script, the others is for the game script to read and the engine script to read to make it easier, so a2.py is the project created inside the engine but both a1.py and a2.py need to read and load the scripts – Benzaboy Jan 18 '18 at 07:26
  • and i dont need to add a path in a2.py coz it can find the files – Benzaboy Jan 18 '18 at 07:27
  • i will repeate - you use name `folder 1` in schema but you don't use name `folder 1` in code. In code you use `tiger` but you don't have this `tiger` in schema. Use REAL names in code and in schema. – furas Jan 18 '18 at 14:58
  • please, could u help? all i want is to know how to fix this, not u ask questions why im doing it! – Benzaboy Jan 18 '18 at 15:26

0 Answers0