Running a python file in Godot

Godot Version

Godot 4.2

Question

Hi all. I’m trying to make a program that goes through a Python file line by line using the PDB python module, then asks some questions about the line currently running (What’s the current value of a variable, what will the program print out, etc) for educational reasons.

So far, I’ve been able to create a basic code editor, which is when you press the button uses OS.execute to run the script in Python. This is working, but if I try running any code that requires an input, well, I can’t do so. I’ve created a thread to run the code, so it doesn’t pause the godot app, but I don’t know if there’s a way to “interact” with the python code while it’s running, so I can get the two running at the same time, thoughts?

Here’s the code I have so far -

extends Control

@onready var thread := Thread.new()

@onready var code_edit = $PanelContainer/HBoxContainer/VBoxContainer/CodeEdit
@onready var label = $PanelContainer/HBoxContainer/VBoxContainer/Label

var output : String :
	set(value):
		output = value
		if label:
			label.set_deferred("text", output)

# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	pass


func _on_button_pressed():
	if thread.is_started():
		thread.wait_to_finish()
	else:
		thread.start(start_python)
	pass # Replace with function body.


func start_python():
		var output_array = []
	
		OS.execute(
			"python", 
			[
				"-c", 
				code_edit.text,
				"-m pdb",
			], 
			output_array,
			true,
			)
		output = output_array[0]