简体   繁体   中英

Call python3 code from python2 code

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?

  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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM