I’ve been trying to write a basic terminal emulation script, because for some reason i’ve got no terminal access on my mac. But to write game engine scripts in blender the console, which usually opens in the terminal you started blender with, is crucial.
For just doing simple things like deleting, renaming etc. I used to execute commands using stream = os.popen(command) and then print (stream.read()). That works fine for most things but not for anything interactive.
Shortly i’ve discovered a new way:
sp = subprocess.Popen(["/bin/bash", "-i"], stdout = subprocess.PIPE, stdin = subprocess.PIPE, stderr = subprocess.PIPE) and then print(sp.communicate(command.encode())). That should spawn an interactive shell that i can use like a terminal, doesen’t it?
But either way i can’t keep the connection open, and using the last example I can call sp.communicate once, giving me the following output(in this case for ‘ls /’) and some errors:
(b'Applications\n[...]usr\nvar\n', b'bash: no job control in this shell\nbash-3.2$ ls /\nbash-3.2$ exit\n').
The second time it gives me a ValueError: I/O operation on closed file.
Sometimes (like for ‘ls’) I only get this error: b'ls\nbash-3.2$ exit\n'.
What does that mean? How can i emulate a terminal with python that allows me to control an interactive shell or run blender and communicate with the console?
Assuming you want an interactive shell that keeps asking for input, you could try the following: