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.
Frank-rene schäfer
source share