Calling Python 2 script from Python 3

前端 未结 7 2160
青春惊慌失措
青春惊慌失措 2020-12-01 02:00

I have two scripts, the main is in Python 3, and the second one is written in Python 2 (it also uses a Python 2 library).

There is one method in the Python 2 script

7条回答
  •  情话喂你
    2020-12-01 02:41

    You could run python2 from bash using subprocess (python module) doing the following:

    From python 3:

    #!/usr/bin/env python3
    import subprocess
    
    python3_command = "py2file.py arg1 arg2"  # launch your python2 script using bash
    
    process = subprocess.Popen(python3_command.split(), stdout=subprocess.PIPE)
    output, error = process.communicate()  # receive output from the python2 script
    

    Where output stores whatever python 2 returned

提交回复
热议问题