From e10841a6c8e967737d690fdb435e259d3e230c36 Mon Sep 17 00:00:00 2001 From: forbes Date: Sat, 14 Feb 2026 21:27:25 -0600 Subject: [PATCH] fix: pass __file__ to exec() in addon Init.py/InitGui.py loading DirMod.run_init() and DirModGui.run_init_gui() use exec(code) to run addon Init.py and InitGui.py files, but don't pass __file__ in the globals dict. Addons that reference __file__ (e.g. Silo) crash with NameError: name '__file__' is not defined. Pass {"__file__": str(init_py)} as the globals argument so __file__ is available in the executed code, matching the behavior of normal Python module loading. --- src/App/FreeCADInit.py | 2 +- src/Gui/FreeCADGuiInit.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/App/FreeCADInit.py b/src/App/FreeCADInit.py index 52ae425b45..471f435dc4 100644 --- a/src/App/FreeCADInit.py +++ b/src/App/FreeCADInit.py @@ -1204,7 +1204,7 @@ class DirMod(Mod): try: source = init_py.read_text(encoding="utf-8") code = compile(source, init_py, 'exec') - exec(code) + exec(code, {"__file__": str(init_py)}) except Exception as ex: Log(f"Init: Initializing {self.path!s}... failed") Log(utils.HLine) diff --git a/src/Gui/FreeCADGuiInit.py b/src/Gui/FreeCADGuiInit.py index 90f9edc01e..eba658a363 100644 --- a/src/Gui/FreeCADGuiInit.py +++ b/src/Gui/FreeCADGuiInit.py @@ -295,7 +295,7 @@ class DirModGui(ModGui): try: source = init_gui_py.read_text(encoding="utf-8") code = compile(source, init_gui_py, "exec") - exec(code) + exec(code, {"__file__": str(init_gui_py)}) except Exception as ex: sep = "-" * 100 + "\n" Log(f"Init: Initializing {target!s}... failed\n")