Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoking Jython from Python (or Vice Versa)

Tags:

python

jython

I'm working on a framework right now, part of which requires Jython. I just added some plotting to it using MatPlotLib, without realizing that MatPlotLib is incompatible with Jython. Since these two parts are pretty isolated, and I would be fine running most of the program in Python and passing a small amount of information to the Jython part (or vice versa), I was wondering if there's a simple way to do this, while maintaining the modular nature of the framework. Ideas?

like image 783
Eli Avatar asked Mar 15 '12 20:03

Eli


People also ask

Does Jython require Python?

Yes. Jython is an implementation of the Python language in Java. We strive to ensure that Jython remains as compatible with CPython as possible. The latest Jython release (2.2) implements the same language as CPython 2.2 and many of the CPython standard library modules.

Is Jython an interpreter?

Interactive experimentation - Jython provides an interactive interpreter that can be used to interact with Java packages or with running Java applications.

Is Jython a version of Python?

Jython is an implementation of the Python language for the Java platform. Jython 2.5 implements the same language as CPython 2.5, and nearly all of the Core Python standard library modules.


1 Answers

I have not used execnet for anything serious, but it seems quite possible that it is a good choice for you. execnet is a Python library for distributed execution across version, platform, and network barriers.

It is not hard to get started. This simple Jython script (that invokes NumPy) worked for me without a hitch:

import execnet

gw = execnet.makegateway("popen//python=python")
channel = gw.remote_exec("""
    from numpy import *
    a = array([2,3,4])
    channel.send(a.size)
""")

for item in channel:
    print item

Output:

3

The documentation includes an example that goes in the opposite direction (a CPython interpreter connecting to a Jython interpreter).

like image 109
mzjn Avatar answered Sep 28 '22 06:09

mzjn