How can I use the Python API to run a Python script, exactly as if it were either opened with .use_module=True or run with or bpy.ops.text.run_script()?
I don't think the the existing answers are about the same question, because I think the use of exec() will necessarily pollute the namespace/globals/locals it's given, likely clobbering existing names. It also seems to have trouble with imports.
Things I've tried:
exec(compile(), {}, {}): Doesn't handle imports of external modules correctly.Text.as_module()(followed by.register()): Doesn't set__file__correctly.ops.script.reload(): Doesn't seem to actually run user scripts.
globals()is unacceptable. For now I'm just switching the context area type to useops.text.run_script(), but that's a bit dirty, so hopefully a better solution is possible. – Will Chen Jun 07 '21 at 14:59