Call python3 code from python2 code

后端 未结 2 1665
刺人心
刺人心 2021-01-02 11:28

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 a

相关标签:
2条回答
  • 2021-01-02 12:09

    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.

    0 讨论(0)
  • 2021-01-02 12:13
    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.

    0 讨论(0)
提交回复
热议问题