Running Python Modules in Python, from Jython

I got into a tricky environment where I work with a Python library, but everything else we have is Java. We want to be able to access and use the Python library from Java, so we started researching and using Jython.

Jython is pretty good and we are importing the Jython Interpreter into our Java program so that we can access most of the library. However, Jython doesn't quite support everything, and we can't do that to get around it. All paths are configured correctly, and there are some modules that we simply cannot import.

So, assuming that we cannot do something that makes the Python library Jython compatible, the next idea is to somehow call Python from the Jython interpreter and make Python run the module (written in Python). Ideally, we could just import the module using Python and then call all methods using Jython (but they were executed in Python).

Does anyone know if this is possible at all, and if so how?

+3


source to share


3 answers


You can use Pyro to send method calls and their return data between Jython and CPython. It is designed so that "objects can talk to each other over a network," but there is no reason why the same technique should not work between two different types of Python on the same machine.



You will find a simple example in the Pyro documentation that shows you everything you need to know to get it up and running.

0


source


You should take a look at



Both are frameworks designed precisely for the required purpose. Also keep an eye out for JyNI (www.jyni.org), which is still in an early state but will significantly improve Jython support for a huge number of Python frameworks.

Using Pyro ( https://pypi.python.org/pypi/Pyro4 ) you can probably solve your problem as well, but keep in mind that it was not designed for this purpose, but for a distributed network. This can lead to subtle problems (like poor performance) making it a poignant solution. This is not necessarily the case, but I would recommend preferring a solution designed for the task if available. However, make this only a hint and a modest warning, since I have not actually compared the named approaches in practice.

0


source


You can avoid Jython altogether and still talk to python through Pyro using Pyrolite in your Java code. Pyrolite is a lightweight client library that allows your Java program to communicate directly with Pyro. Will work as long as your Java program is only a Pyro client.

0


source







All Articles