Executing python script and debug window freezes

Godot Version

4.0.3

Question

Hello,
I’m trying to learn how to run godot projects and python scripts simutaneosly, and make them transfer data one into another.
I’ve put a python virtual enviroment inside the project’s directory according to this tutorial (although I’m using Windows instead of mac) → https://www.youtube.com/watch?v=z2MHuWEDUNw.
I’ve wrote a python script which worked outside of the project.
In the gdscript of the main scene, it executes the script with OS.execute()
Here’s the script of the scene:
~```
extends Node2D

var DIR = OS.get_executable_path().get_base_dir()
var pySrcDIR= DIR.path_join(“res://PythonFiles/venv/source/”)
var interpeter_path = DIR.path_join(“res://PythonFiles/venv/Scripts/python.exe”)

func _ready():
if !OS.has_feature(“standalone”):
interpeter_path = ProjectSettings.globalize_path(“res://PythonFiles/venv/Scripts/python.exe”)
pySrcDIR= ProjectSettings.globalize_path(“res://PythonFiles/venv/source”)
printArgs(“this”,“is”,“testing”)

func printArgs(msg1=“”,msg2=“”,msg3=“”):
print(pySrcDIR.path_join(“test.py”))
print(interpeter_path)
var output =
var errMsg = OS.execute(interpeter_path,[pySrcDIR.path_join(“test.py”),msg1,msg2,msg3],output)
print(errMsg == OK)
func _process(delta):
pass```
~

Try removing the “~” in your messages before the “```” to see if the code formats correctly, but I tried this on my machine and it seems to have worked. What error are you getting? If there’s no error, what do you expect to get vs what you’re actually getting? I’m on Linux so it’s a bit different, but here is my code.

extends Node2D

var dir = OS.get_executable_path().get_base_dir()
var py_int_path = dir.path_join("pythonfiles/venv/bin/python")
var py_script = dir.path_join("pythonfiles/test.py")

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	if !OS.has_feature("standalone"):
		py_int_path = ProjectSettings.globalize_path("res://pythonfiles/venv/bin/python")
		py_script = ProjectSettings.globalize_path("res://pythonfiles/test.py")
	_py_print("Hello", " ", "World!")

func _py_print(arg1, arg2, arg3):
	var output = []
	var testmsg = OS.execute(py_int_path, [py_script, arg1, arg2, arg3], output)
	print(output)
	print(testmsg)
	print("Finished")

And here’s my test.py

import sys
import tkinter as tk
from tkinter import messagebox

def main():
    args = sys.argv
    print(args[1], args[2], args[3])
    root = tk.Tk()
    root.withdraw()
    messagebox.showwarning(args[1], args[3])
    msg = args[1] + args[2] + args[3]
    return msg

if __name__ == "__main__":
    main()

This just also creates a messagebox so I could see it was working, but it also returns the following in the debug output:

["Hello   World!\n"]
0
Finished

Hello, thank you for commenting!
I’ve forgot to edit the post. The notification script worked, so technically there’s no error. But the debug window is freezing while the notification stays afloat. When the notification is gone, the debug window stops freezing.

The main issue here is how do I make the project not freezing while the python script is running?

(By the way, for some reason an Edit button does not appear on my post, so I don’t know how to edit the post, Sorry.)

Oh it stops because OS.execute blocks by default.

Note: The Godot thread will pause its execution until the executed command terminates. Use Thread to create a separate thread that will not pause the Godot thread, or use create_process to create a completely independent process.

1 Like

Ah, Thank you! Solved.

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