13

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 2

18
  1. Talk over a pipe or socket

  2. Enable such python 3 features as you can from __future__ or use a library like six to write code which is compatible with both.

  3. 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.

Sign up to request clarification or add additional context in comments.

10 Comments

@MatthewG So, why do you want to use python 3?
I 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.
@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.
I 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.
@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.
|
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 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 threads
In 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.