From 3e57133c84a2a505bcb9bbe5ef1fc3dba30d35c2 Mon Sep 17 00:00:00 2001 From: LeoBorcherding Date: Mon, 11 May 2026 06:16:05 -0500 Subject: [PATCH] fix: set __spec__ on mod stubs so importlib.util.find_spec doesn't raise Manually-injected sys.modules entries have __spec__=None by default. importlib.util.find_spec() raises ValueError when it finds a module in sys.modules with __spec__=None (transformers.utils.import_utils hits this when checking if torchao is available). Give every stub a minimal ModuleSpec(name, loader=None, is_package=True) to satisfy find_spec. --- studio/backend/core/training/worker.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/studio/backend/core/training/worker.py b/studio/backend/core/training/worker.py index 8db2686f15..8701b16f1e 100644 --- a/studio/backend/core/training/worker.py +++ b/studio/backend/core/training/worker.py @@ -1104,10 +1104,17 @@ def run_training_process( # any attempt to import a submodule (e.g. "torch.distributed.tensor._foo") # raises "is not a package" because Python checks __path__ before looking # in sys.modules for the child. + import importlib.machinery as _ilm + def _make_mod_stub(mod_name): m = _types.ModuleType(mod_name) m.__path__ = [] # marks this as a package to the import system m.__package__ = mod_name + # importlib.util.find_spec() raises ValueError when __spec__ is None + # on a module that's already in sys.modules (our manually-injected + # stubs). Give every stub a minimal ModuleSpec so find_spec() returns + # a spec object (non-None) instead of raising. + m.__spec__ = _ilm.ModuleSpec(mod_name, loader=None, is_package=True) def _ga(attr, _m=m, _n=mod_name): if attr.startswith("__"): raise AttributeError(attr)