From d86f8dcef6959e0199929d4b6cbe0a5f3f60a0ae Mon Sep 17 00:00:00 2001 From: Splines Date: Tue, 4 Feb 2025 17:46:53 +0100 Subject: [PATCH] Fix module exec error by adding it to sys.modules beforehand --- manimlib/extract_scene.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/manimlib/extract_scene.py b/manimlib/extract_scene.py index 28e22256..88e77091 100644 --- a/manimlib/extract_scene.py +++ b/manimlib/extract_scene.py @@ -174,7 +174,17 @@ def insert_embed_line_to_module(module: Module, run_config: Dict) -> None: # Execute the code, which presumably redefines the user's # scene to include this embed line, within the relevant module. - code_object = compile(new_code, module.__name__, 'exec') + # Note that we add the user-module to sys.modules to please Python builtins + # that rely on cls.__module__ to be not None (which would be the case if + # the module was not in sys.modules). See #2307. + if module.__name__ in sys.modules: + log.error( + "Module name is already used by Manim itself, " + "please use a different name" + ) + sys.exit(2) + sys.modules[module.__name__] = module + code_object = compile(new_code, module.__name__, "exec") exec(code_object, module.__dict__)