Pygame segmentation fault with import depending on position in code?

I know these segmentation fault questions are common, but I couldn’t find answer and I am pulling my hair out as the code works without issue in some parts of the code, but not others or in some cases it works the first time then, the next day, it falls over.

Basically, I am importing spritesheets for a simply pygame game. With help, I wrote a small class which allows spritesheets to be imported and, through a few methods turns them into a list using the json file. That program is as follows:

import pygame
import json


pygame.init()

class Spritesheet:
    def __init__(self, filename):
        self.filename = filename
        self.sprite_sheet = pygame.image.load(filename).convert()
        self.data_addr = self.filename.replace('png', 'json')
        
        with open(self.data_addr, encoding = 'utf8') as f:
            self.data = json.load(f)
        f.close()
        
      
    def check_file(self):
        
          print(self.data)
    
    def get_sprite(self, x, y, w, h):
        sprite = pygame.Surface((w, h))
        sprite.set_colorkey((0,0,0))
        sprite.blit(self.sprite_sheet,(0, 0),(x, y, w, h))
        return sprite

    def parse(self, index):
        sprite = self.data['frames'][index]['frame']
        x, y, w, h = sprite["x"], sprite["y"], sprite["w"], sprite["h"]
        image = self.get_sprite(x, y, w, h)
        return image
                    
    def dict_len(self):
        length = len(self.data['frames'])
        return length

This has worked fine in the main code. I have a player class, an enemy class etc and, upon initiallising, this class is called to create a list of pngs for the graphics (that I can step through etc.) A pertinent cut of the player class is as follows:

class Player(pygame.sprite.Sprite):
    def __init__(self, player_posx, health):
        super().__init__()
        
        #right facing sprites
        self.spritesr = Spritesheet('../graphics/player/players.png')
        self.player_spritesr = []
        for value in range(self.spritesr.dict_len()):
            self.player_spritesr.append(self.spritesr.parse(value))
            
        # left facing sprites
        self.spritesl = Spritesheet('../graphics/player/playersl.png')
        self.player_spritesl = []
        for value in range(self.spritesl.dict_len()):
            self.player_spritesl.append(self.spritesl.parse(value))
            
        #static sprites
        self.sprites_static = Spritesheet('../graphics/player/player_static.png')
        self.player_static = []
        for value in range(self.sprites_static.dict_len()):
            self.player_static.append(self.sprites_static.parse(value))
        
        self.image_index = 0
        self.image = self.player_spritesr[self.image_index]
        self.rect = self.image.get_rect()

In this class it works fine with the Spritesheets class. However, when I decided to create new file to import all the graphics in a separate file, I get the segmentation error. The code is simply:

from spritesheets import Spritesheet

test = Spritesheet('../graphics/player/playersl.png')

Calling exactly the same class with the same argument, but I get an error saying:
Fatal Python error: (pygame parachute) Segmentation Fault
Python runtime state: initialized

I really don’t understand at all, especially why it runs fine in one file but falls over everywhere else.

1

The likely problem is that you are doing your import before you have called pygame.init(), so pygame has not been initialized.

Consider doing this in your second file:

from spritesheets import Spritesheet

def get_sprites():
    return [
        Spritesheet('../graphics/player/players.png'),
        Spritesheet('../graphics/player/playersl.png'),
        Spritesheet('../graphics/player/players_static.png')
    ]

Now you can put your import at the top, and call

sprites = xxx.get_sprites()

after you have initialized pygame.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật