Can’t get attribute “Preset” (custom class) on <module '__main__' from "/path"

I am writing a flask app and have a setup function that will initialise a list of objects and write them to a file using pickle. The setup function works fine when I run the script through the terminal, however trying to load the pickled file results in the title error:

Can’t get attribute “Preset” (custom class) on <module ‘main‘ from ‘/Users/angoose/PycharmProjects/pythonProject1/.venv/bin/Flask’.

from flask import Flask, render_template, request
import pickle


global filename
filename = "static/presets.pkl"



class Preset:
    def __init__(self, name: str, displayName: str, layout: str, stringData: list):
        self.name = name.upper()
        self.displayName = displayName
        self.layout = layout
        self.stringData = stringData
    def __str__(self):
        return f"{self.name},{self.displayName},{self.layout},{self.stringData}"


def setup() -> None:
    try:
        with open(filename, "rb") as f:
            presets = pickle.load(f)
        if not type(presets) == list or not len(presets) == 3:
            raise EOFError
        else:
            print("hurray!")
    except EOFError:
        # file is empty
        six = Preset("Fender", "Stratocaster", "6", ['E', 'A', 'D', 'G', 'B', 'E'])
        four = Preset("JP60", "JP60", "4+2", ['E', 'A', 'D', 'G', 'B', 'E'])
        three = Preset("LPStandard", "Les Paul Standard", "3+3", ['Eb', 'Ab', 'Db', 'Gb', 'Bb', 'Eb'])
        with open(filename, "wb") as f:
            pickle.dump([six, three, four], f, pickle.HIGHEST_PROTOCOL)


def save(obj) -> None | int:
    # File mode "ab" for "append bytes"
    with open(filename, "rb") as f:
        presets = pickle.load(f)
        if not type(presets) == list:
            return 1

    with open(filename, "wb") as f:
        presets.append(obj)
        pickle.dump(presets, f, pickle.HIGHEST_PROTOCOL)
    return None


def load():
    with open(filename, "rb") as f:
        return pickle.load(f)



setup()
app = Flask(__name__)


@app.route("/")
def index():
    name = request.args.get("name")
    return render_template("index.html", name=name)


@app.route("/tune")
def tune():
    x = Preset("jp60", "Stella","4+2","['E','A','D','G','B','E']")
    save(x)
    y = load()



    return render_template("tune.html", test=y)


@app.route("/metronome")
def metronome():
    return render_template("metronome.html")

I have looked around to see if others have had this issue and it looks like most times the issue is that pickle cannot find the class to reconstruct it, as pickle does not save the class itself. I have so far tried: using dill instead, moving the class definition around, checking if Flask has the right requirements (pickle module) and none have worked. The fact that pickle can work correctly in normal python (commenting out the flask sections) makes me think that this is probably flask and pickle not mixing well, which I also searched and found little to help.

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