0

So, i'm trying to import my main.py working file in pygame to another file called player.py. However,i get an error of Exception has occurred: ImportError cannot import name 'Level' from partially initialized module 'level' (most likely due to a circular import) Code in level.py:

import pygame
from tiles import Tile
from map import level_map,tile_size,screen_width
from player import Player
class Level:
    def __init__(self,level_data,surface):
        self.display_surface=surface
        self.level_building(level_data)
        self.world_shift=0

code in Player.py:

import pygame

from level import *
from tiles import Tile
from main import *
class bullets(pygame.sprite.Sprite):
    def __init___(self,pos):
        super().__innit__()
        self.image=pygame.Surface((8,8))
        self.rect=self.image.get_rect(topleft=(pos))
        self.direction=0
        self.speed=8
        self.distcount=0

code in main.py:

import pygame   
from colors import *  
from map import *  
from colors import *
from tiles import Tile
from level import Level
from player import *
#initialisation for pygame
pygame.init()
screen=pygame.display.set_mode((screen_width,screen_height))
sky_surface=pygame.image.load('Graphics/background.jpg')
pygame.display.set_caption('Test 1')
clock= pygame.time.Clock()
level=Level(level_map,screen)

Sorry for how long the code is i don't know whats important or not but if you'd like to see more to know the problem i'll add on more

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • I think your circular import is that `main.py` is importing everything from `player.py` which is importing everything from `main.py`. I'd recommend against `import *` anyway. – import random Aug 25 '22 at 07:52
  • how would i go about importing everything though? i only imported screen and level which were the variables i needed but still results in the same error – 22S3-C alden limyuhong Aug 25 '22 at 12:35
  • It's not clear from your example why you need so many imports. You could start by disabling all imports except pygame. Instead of importing the screen_width, you should pass that as a parameter if an object needs to know. – import random Aug 26 '22 at 00:38
  • well i've removed unnessecary imports now and only used imports my code could not run without, but still im getting the error – 22S3-C alden limyuhong Aug 27 '22 at 01:00
  • Please [edit] your question to include a [mre], otherwise it's not possible to specifically help you. See this [question](https://stackoverflow.com/q/7336802/2280890) on avoiding circular imports. – import random Aug 28 '22 at 09:11
  • I don't know what code you would need, but it's when i import main in another file. I can import in between the other file, but files simply cannot import main. I assume it would be because main already has imported the other files – 22S3-C alden limyuhong Aug 28 '22 at 09:44
  • For assistance in crafting a question that us answerable, please review [ask]. You may also find it beneficial to take the [tour]. – import random Aug 28 '22 at 13:21

0 Answers0