Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Python 2 script from Python 3

Tags:

python

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 I want to call from the Python 3 script, but I don't know how to cross this bridge.

like image 813
Gary Ye Avatar asked Jan 09 '15 15:01

Gary Ye


People also ask

Can I use Python 2 and 3 together?

We can have both Python 2 and Python 3 installed on any Windows or Linux device. We can either create different environments on different IDEs to use the versions separately or use the following ways to run them using the command prompt.

How do I run a Python script in Python 2?

Using the python Command To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!


2 Answers

Calling different python versions from each other can be done very elegantly using execnet. The following function does the charm:

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) 

result in

Hello Mr Bear! Hello Mrs Wolf! 

What happened is that a 'gateway' was instantiated waiting for an argument list with channel.receive(). Once it came in, it as been translated and passed to my_function. my_function returned the string it generated and channel.send(...) sent the string back. On other side of the gateway channel.receive() catches that result and returns it to the caller. The caller finally prints the string as produced by my_function in the python 3 module.

like image 56
Frank-Rene Schäfer Avatar answered Sep 18 '22 18:09

Frank-Rene Schäfer


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

like image 28
mikelsr Avatar answered Sep 21 '22 18:09

mikelsr