What's the closest I can get to calling a Python function using a different Python version?
<p>Say I have two files:</p>

<pre><code># spam.py
import library_Python3_only as l3

def spam(x,y)
    return l3.bar(x).baz(y)
</code></pre>

<p>and</p>

<pre><code># beans.py
import library_Python2_only as l2

...
</code></pre>

<p>Now suppose I wish to call <code>spam</code> from within <code>beans</code>. It's not directly possible since both files depend on incompatible Python versions. Of course I can <code>Popen</code> a different python process, but how could I pass in the arguments and retrieve the results without too much stream-parsing pain?</p>

<p>You could create a simple script as such :</p>

<pre><code>import sys
import my_wrapped_module
import json

params = sys.argv
script = params.pop(0)
function = params.pop(0)
print(json.dumps(getattr(my_wrapped_module, function)(*params)))
</code></pre>

<p>You'll be able to call it like that :</p>

<pre><code>pythonx.x wrapper.py myfunction param1 param2
</code></pre>

<p>This is obviously a security hazard though, be careful.</p>

<p>Also note that if your params are anything else than string or integers, you'll have some issues, so maybe think about transmitting params as a json string, and convert it using <code>json.loads()</code> in the wrapper.</p>

python compatibility popen