How to run python with OS.execute_with_pipe function?

Godot Version

Godot 4.3.0 beta-1 windows

Question

This is code adapted from #89206’s example, which only modifies the program that is executed, but it is now completely useless, and it does not write any characters in the TextEdit.

extends Control


var pipe
var thread


func _ready():
	var info = OS.execute_with_pipe("python.exe", [])
	pipe = info["stdio"]
	thread = Thread.new()
	thread.start(_thread_func)
	get_window().close_requested.connect(clean_func)


func _thread_func():
	while pipe.is_open() and pipe.get_error() == OK:
		_add_char.call_deferred(char(pipe.get_8()))
	print(pipe.get_error())

func _add_char(c):
	$TextEdit.text += c
	$TextEdit.scroll_vertical = $TextEdit.get_v_scroll_bar().max_value


func _on_line_edit_text_submitted(new_text: String) -> void:
	var cmd = new_text + "\n"
	var buffer = cmd.to_utf8_buffer()
	pipe.store_buffer(buffer)
	$LineEdit.text = ""


func clean_func():
	pipe.close()
	thread.wait_to_finish()

Here is the scene tree:
屏幕截图 2024-06-08 204003
How can I make it correct?

OS.execute_with_pipe("cmd.exe", ["/c","python.exe" ])

It doesn’t work yet.

OS.execute_with_pipe("cmd.exe", ["/c", "python.exe"])

There is also a stderr is it throwing an error that you can’t see?

Not at all. There’s nothing in stderr

I think interactive python command line doesn’t use normal stdio, this code works for other programs.

but if you run a python script it works fine

I’m having issues running the code as well on Linux.

Ive made my comments on github. Seems the execute command does not return a valid pipe.

Thank you very much.

Btw I’m also on Linux. I was only able to capture python interactive shell to file with 3> or &>, meaning it’s some of its output is not on stdio. If I tried to do a redirect to stdio In Godot with something like 3>&1 it still wouldn’t capture the python shell output, but it would crash the game if you did an input.

Hi, I have it working on Linux, though not sure what was causing all my problems before. If anyone wants download attached project. I am using bash as the shell.
When you run it, you can type a command in the LineEdit box and press Enter. E.g. type something pwd or ls. I also tried ping yahoo.com works but no way to break out of it (ie ctrl-c does now work). Pretty cool though.

2 Likes

I had the same result with bash and sh, but the original post was trying to use the python shell, which doesn’t use the standard streams, I have only confirmed this by using Linux redirects greater then the stderr stream.

Depending what you want to do but you need an input() loop of sorts.
E.g.: create a small python stub app input.py

# input.py
print("Ready! Enter Python Command: ")
while True:
    data = input(">>")
    if 'Exit' == data:
        break
    print(eval(data))
print("Done")

In Godot run python3 with the input.py as argument

# godot app
		args=["/home/user1/godot/pipetest/input.py"]
		info = OS.execute_with_pipe("/usr/bin/python3", args)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.