Call Python 2 script from Python 3

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

There is one method in the Python 2 script that I want to call from the Python 3 script, but I don't know how to cross this bridge.

+8
python
source share
4 answers

You can start python2 from bash using a subprocess (python module) by 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 

In the case where the output stores all returned python 2

+7
source share

Calling different versions of python from each other can be done very elegantly using "execnet". The following function is lovely:

 import execnet def call_python_version(Version, Module, Function, ArgumentList): gw = execnet.makegateway("popen//python=python%s" % Version) channel = gw.remote_exec(""" from %s import %s as the_function channel.send(the_function(*channel.receive())) """ % (Module, Function)) channel.send(ArgumentList) return channel.receive() 

Example: A my_module.py written in Python 2.7:

 def my_function(X, Y): return "Hello %s %s!" % (X, Y) 

Then the following function calls

 result = call_python_version("2.7", "my_module", "my_function", ["Mr", "Bear"]) print(result) result = call_python_version("2.7", "my_module", "my_function", ["Mrs", "Wolf"]) print(result) 

will result in

 Hello Mr Bear! Hello Mrs Wolf! 

It so happened that a “gateway” was created for the argument list with channel.receive() . As soon as he entered, he was transferred and transferred to my_function . my_function returns the string that it generated, and channel.send(...) sent the string back. On the other hand, the channel.receive() gateway catches this result and returns it to the caller. The caller finally prints the line created by my_function in the python 3 module.

+3
source share

I am running my python code with python 3, but I need a tool (ocropus) written with python 2.7. I tried all these options with the subprocess for a long time and saved errors, but the script was not executed. From the command line, this works fine. So I finally tried something simple that worked, but I did not find this in my online search versions. I put the ocropus command inside a bash script:

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

I am calling a bash script with a 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) 

It really gives the ocropus script its own little world, which apparently needs it. I post this in the hope that he will save someone else some time.

0
source share

Note. This happened when running my python 2.xs / w in IDL liclipse. When I ran it from a bash script on the command line, it had no problem. Here is the problem and solution I had when mixing python 2.x and 3.x scripts.

I am running a python 2.6 process and should call / execute a python 3.6 script. The PYTHONPATH environment variable was set to 2.6 python s / w, so it was choking on followng:

 File "/usr/lib64/python2.6/encodings/__init__.py", line 123 raise CodecRegistryError,\ 

This caused a crash of 3.6 python script. Therefore, instead of directly accessing program 3.6, I created a bash script that encoded the PYTHONPATH environment variable.

 #!/bin/bash export PYTHONPATH= ## Now call the 3.6 python scrtipt ./36psrc/rpiapi/RPiAPI.py $1 
0
source share

All Articles