I am designing a GUI using the wxPython toolkit, which means it's being written in python2. However, I want to use python3 for the actual application code. How would I go about calling my python3 code from the GUI?
2 Answers
Talk over a pipe or socket
Enable such python 3 features as you can from
__future__or use a library likesixto write code which is compatible with both.Don't do this.
Finally, are you sure you can't use wxPython in Python 3? There's nothing in the online docs saying you can't.
10 Comments
You can run the application code as a shell script.
from subprocess import call
exit_code = call("python3 my_python_3_code.py", shell=True)
You can also pass in terminal arguments as usual.
arg1 = "foo"
arg2 = "bar"
exit_code = call("python3 my_python_3_code.py " + arg1 + " " + arg2, shell=True)
If you want to collect more information back from the application you can instead capture stdout as describe here: Capture subprocess output
If you want to fluidly communicate in both directions a pipe or socket is probably best.
2 Comments
shell=True is unnecessary. subprocess.call() waits for the script to finish -- it will hang the GUI. OP probably wants to run multiple commands i.e., python3 process should be started once and then you should use pipe/socket other IPC methods as suggested in @Marcin's answer. Here's Gtk code example that shows how to read subprocess output in a GUI application (using threads or async.io). Here's Tkinter code example (async) and using threadsshell=True is necessary when you have arguments, otherwise there is an error: FileNotFoundError: [Errno 2] No such file or directory. I used it to call Python 2 code from Python 3.