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?
Asked
Active
Viewed 2.1k times
2 Answers
18
Talk over a pipe or socket
Enable such python 3 features as you can from
__future__
or use a library likesix
to 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.

Marcin
- 48,559
- 18
- 128
- 201
-
Here is a list expanding item no. 1: [Main Inter-Process Communication methods](http://en.wikipedia.org/wiki/Inter-process_communication#Main_IPC_methods). – Tadeck Jul 15 '13 at 22:49
-
A version of wxPython that supports python3 is still in development. I shudder to think how far away a decent GUI builder like wxGlade is that supports a python3 version of wxPython. – Matthew G Jul 15 '13 at 23:00
-
2@MatthewG So, why do you want to use python 3? – Marcin Jul 15 '13 at 23:02
-
6I want to use python3 because it's the latest version of the language. It's been around since 2008 and there's really no reason to develop a brand new application in an old language. – Matthew G Jul 15 '13 at 23:33
-
2@MatthewG Latest and greatest is not a reason. Python 2 is not some obsolete language - as your situation demonstrates, it is a language with active development of libraries, and substantial numbers of libraries that do not exist for Python 3. – Marcin Jul 15 '13 at 23:54
-
7I made the decision to learn Python in 2011, before I was aware of the amount of legacy python2 code still around. I've been told countless times to just use python2. In my first week of learning python3, I was actively scolded by someone for even considering writing python3 code. I'm used to this. It's incredibly disappointing, and frustrating that most people seem to just accept this chicken and egg situation lying down. I love Python, but the community is pretty terrible to be honest. – Matthew G Jul 16 '13 at 00:28
-
6@MatthewG You're the only person I've ever heard say that. In any case, there's no reason why you can't just use python 2 - between compatibility libraries, and the similarity of the languages, this shouldn't be an unreasonable suggestion. You don't have to swear loyalty to one version or another. – Marcin Jul 16 '13 at 01:00
-
1I know that it's four years later, and it is quite possible that several things have changed between your last comment and this one, but to update this conversation, I would just like to cite [the docs](https://wiki.python.org/moin/Python2orPython3#What_are_the_differences.3F) stating that 2.x is legacy. – Nathan Smith Jul 31 '17 at 19:51
-
@NathanSmith Yeah but they keep saying that, and yet here we are with python 3 adoption still not especially great. But even 4 years ago one of my recommended solutions was just use a library available in python 3. You can use Python 3 if you want, and you can accept the relative limitation of libraries (which is admittedly less). – Marcin Jul 31 '17 at 20:16
-
1Perhaps this was inherently true, but several of these comments aged very poorly. – Kartik Chugh Aug 06 '21 at 06:19
8
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`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)](https://gist.github.com/zed/8a255e81eb87431c0e63). Here's [Tkinter code example (async)](https://gist.github.com/zed/9294978) and [using threads](https://gist.github.com/zed/42324397516310c86288) – jfs Aug 01 '15 at 15:06
-
1In fact `shell=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. – baptx Jul 01 '21 at 12:50