Python script runs successfully in Thonny but returns NameError: name 'Snowflake' isn't defined if run from app list



  • I created this simple app to play with animations

    import M5
    import random
    
    class Snowflake:
        def __init__(self):
            self.MAX_WIDTH = 320
            self.MAX_HEIGHT = 240
            self.radius = random.randint(2, 4)
            self.velocity = random.randint(2, 5)
            self.x = random.randint(0, self.MAX_WIDTH)
            self.y = random.randint(0, self.MAX_HEIGHT)
        
        def fall(self):
            self.y += self.velocity
            if self.y > self.MAX_HEIGHT:
                self._reset()
        
        def _reset(self):
            self.radius = random.randint(2, 4)
            self.velocity = random.randint(2, 5)
            self.x = random.randint(0, self.MAX_WIDTH)
            self.y = 2
    
    def setup():
        global SNOWFLAKES_COUNT
        global snow
        global circles
        
        SNOWFLAKES_COUNT = 80
        snow=[]
        circles=[]
        
        M5.begin()
        M5.Widgets.fillScreen(0x66b8f7)    
        for _ in range(SNOWFLAKES_COUNT):
            snow.append(Snowflake())
            circles.append(M5.Widgets.Circle(0, 0, 0, 0xffffff, 0xffffff))
    
    def loop():
        M5.update()
        head = M5.Widgets.Circle(160, 144, 25, 0xffffff, 0xffffff)
        body = M5.Widgets.Circle(160, 199, 39, 0xffffff, 0xffffff)
        nose = M5.Widgets.Triangle(161, 143, 122, 150, 161, 148, 0xEA6511, 0xEA6511)
        left_eye = M5.Widgets.Circle(152, 135, 2, 0x000000, 0x000000)
        right_eye = M5.Widgets.Circle(167, 135, 2, 0x000000, 0x000000)
        for index, s in enumerate(snow):
            s.fall()
            c = circles[index]
            c.setRadius(r=s.radius)
            c.setCursor(x=s.x, y=s.y)
    
    if __name__ == '__main__':
      try:
        setup()
        while True:
          loop()
      except (Exception, KeyboardInterrupt) as e:
        try:
          from utility import print_error_msg
          print_error_msg(e)
        except ImportError:
          print("please update to latest firmware")
    

    If i run this code from Thonny pressing the run button it works, but if i run it from the the app list, i get this error:

    MPY: soft reboot
           _  __ _               
     _   _(_)/ _| | _____      __
    | | | | | |_| |/ _ \ \ /\ / /
    | |_| | |  _| | (_) \ V  V / 
     \__,_|_|_| |_|\___/ \_/\_/  2.0.3
    
    _click_event_handler
    _click_event_handler
    Traceback (most recent call last):
      File "apps/snowman.py", line 60, in <module>
      File "apps/snowman.py", line 40, in setup
    NameError: name 'Snowflake' isn't defined
    
    MicroPython v1.22.0-dirty on 2024-03-21; M5STACK Core2 with ESP32(SPIRAM)
    Type "help()" for more information.
    

    Anyone knows the cause of this error and how to resolve it?