The closest and easiest thing to this I can think of is making yourself an operator that runs a text block from the text editor:
import bpy
class RunFromTextEditor(bpy.types.Operator):
"""Run from text editor"""
bl_idname = "wm.run_from_tex_editor"
bl_label = "Run from Text Editor"
def execute(self, context):
override = context.copy()
override["edit_text"] = bpy.data.texts['run_from_operator.py']
with context.temp_override(**override):
bpy.ops.text.run_script()
return {'FINISHED'}
def register():
bpy.utils.register_class(RunFromTextEditor)
def unregister():
bpy.utils.unregister_class(RunFromTextEditor)
if __name__ == "__main__":
register()
Once you register it, you will have an operator that you can reach from search menu and it will run anything you have in a text block named "run_from_operator.py" so you can run code that is in the text editor from any context in the UI that you call the operator from. Might be useful to print the context for example to help you find out, what you need to override some operator's context, or just simply test some code in another context.