Calling Python 2 script from Python 3

前端 未结 7 2161
青春惊慌失措
青春惊慌失措 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:25

    I am running my python code with python 3, but I need a tool (ocropus) that is written with python 2.7. I spent a long time trying all these options with subprocess, and kept having errors, and the script would not complete. From the command line, it runs just fine. So I finally tried something simple that worked, but that I had not found in my searches online. I put the ocropus command inside a bash script:

    #!/bin/bash
    
    /usr/local/bin/ocropus-gpageseg $1
    

    I call the bash script with subprocess.

    command = [ocropus_gpageseg_path,  current_path]
    process = subprocess.Popen(command,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    output, error = process.communicate()
    print('output',output,'error',error)
    

    This really gives the ocropus script its own little world, which it seems to need. I am posting this in the hope that it will save someone else some time.

提交回复
热议问题