Can python modules be used from Java programs using Jython without modification? - java

Can Python code be used from Java using Jython, without modifying the Python code in a way which will prevent it from working correctly in CPython?
If yes, what steps would have to be taken (in the Java code)?
If not, what are the reasons that this cannot be done (so far)?

Python modules can depend on certain Python versions (e.g. Python 3 vs Python 2 and even may require a minimum Python version (e.g. 2.6) in case of using dedicated language features introduced in some Python version)
Python modules may depend on C extensions which won't work with Jython
Python modules may use CPython features that are not available in Jython
In general: most Python-only code should work with Jython - however like in all cases: you have to test, test, test. Good written modules provide unittests - so you should try to run the tests from Jython and see what's happening.

Related

Using the original python packages instead of the jython packages

I am trying to create a hybrid application with python back-end and java GUI and for that purpose I am using jython to access the data from the GUI.
I wrote code using a standard Python 3.7.4 virtual environment and it worked "perfectly". But when I try to run the same code on jython it doesn't work so it seems that in jython some packages like threading are overwritten with java functionality.
My question is how can I use the threading package for example from python but in jython environment?
Here is the error:
Exception in thread Thread-1:Traceback (most recent call last):
File "/home/dexxrey/jython2.7.0/Lib/threading.py", line 222, in _Thread__bootstrap
self.run()
self._target(*self._args, **self._kwargs)
Since you have already decoupled the application i.e using python for backend and java for GUI, why not stick to that and build in a communication layer between the backend and frontend, this layer could either be REST or any Messaging framework.
I think one issue you might be facing is that you are running on Jython 2.7.0 (~Python 2.7 compatible) but your code is written for CPython 3.7.4. So you might be facing Python 2 vs 3 issues.
The threading module in Jython is indeed different from CPython i.e. it is making use of JVM threading. However the intension is to be API compatible with Python 2.7 code so if it doesn't run that should be considered a bug.
My first suggestion would be try Jython 2.7.1 it contains many fixes over 2.7.0 https://www.jython.org/download however I suspect it still might not work. Check if your code runs on CPython 2.7 https://www.python.org/downloads/release/python-2716/ if it does then that sounds like a Jython bug. Open a ticket with details to reproduce at https://bugs.jython.org/

Using GraalPython as a Jython replacement

I wonder if it is possible to use GraalPython as a Java library to interpret Python code on a standard JVM. If so, would it be a good choice to replace Jython (which only supports Python 2.7)?
Is this possible without importing the entire GraalVM project? I expect only Truffle and the Python interpreter built on top of it should be necessary.
If this is not possible, are there any good Java implementations of Python 3 available?
You should be able to run any GraalVM language on any JDK as their are just Java programs. However, the performance will be affected a lot. Moreover, languages like python consist of additional resources (standard library files, etc.) that you would have pull from GraalVM too.
This document about GraalVM JavaScript discusses this in more detail and describes how to run GraalVM JavaScript on stock JDK without compromising the performance. Some of it can be applicable to GraalPython.
https://github.com/graalvm/graaljs/blob/master/docs/user/RunOnJDK.md
Tl;dr: it will be much easier to use GraalVM. It's full JDK distribution. You are not missing on anything. If you can't, there are some ways.

Can Python invoke the Java Framework?

Can Python invoke the Java Framework?
I want to know whether a Python project can invoke a Java Framework, I find a Java Framework in GitHub, whether I can use it in my Python project?
Jython
Jython is one method of calling Java from python -- actually, you run your Python inside Java JVM. This gives you access to almost any Java that runs on JVM, but comes with many limitations.
Because Jython is running python inside the JVM, this gives you acess to almost any Java library. However, you are very restricted in what Python you can use: you can only use Python 2.7, and can import pure Python libraries only (compiled Python libraries with C will not run on Jython).
For an example of a project that uses Jython: Processing.py runs on Jython in order to access the Processing Java API and its ecosystem of Java libraries.
https://github.com/jdf/processing.py
Note that Jython 2 and its docs are quite old, and that the developers are uncertain if / when Jython 3 will be released.
https://github.com/jython/jython3
py4j
py4j is a different approach -- it is "A Bridge between Python and Java" and lets native python code access separate Java running in a separate JVM. Note however that the python and Java code must be running in parallel and communicating through a gateway interface. This is communication between separately running processes -- you are not spinning up a JVM from Python or inside Python.
For example: on the JVM side pass myObject to a new GatewayServer(myObject); on the Python side create a JavaGateway() Python object and use it to communicate with the Java myObject.
Normally Python and Java have their own interpreters/VM's and cannot be shared. It is possible to use Jython but has limitations (fe. python version and support/compatibility with other python packages).
The interpreter and JVM do not match: Java has strict typing, python not. Java compiles and run, python is an interpreter and can change code in runtime (if you want). These are extra challenges why putting all in a same environment is very complex.
There are possibilities like a client/server architecture, but the feasability depends on the level of the framework.
Most of the time low level frameworks are optimized to run directly inside your application process. Any loose coupling will introduce performance and security and compatibility issues. Just think about how reflection will work or multiple inheritance.
If it is a high level framework (fe able to run stand alone) it is more feasable to use some sort of client/server. But still you have to develop a lot for it.
Industry standard is just to implement the framework of your desire in the language you want, then you can get also all the benefits of your platform.

Use python library in java code

There is some library called pymorphy written in python. Unfortunately, for java there is not any library with the similar functionality - natural language processing for Russian lang. So I need to invoke some methods of pymorphy library from Java code.
First I've tried to solve this problem with Jython. But I've spent 2 days and the goal was not accomplished because python modules cdb, bsddb3, sqlite are written in C and they will not work with Jython.
Now I want to run some python light-weight server with pymorphy for handling request from Java code.
How could I implement this kind of java-python interaction with the maximum production performance? Or is there more simple way to call python from java?
Try Jepp, "Java Embedded Python". http://jepp.sourceforge.net/
I haven't used it beyond small projects, but it works as advertised, allowing one to call CPython transparently from Java. If you have the opposite problem, needing to call Java from CPython, definitely check out JPype. I've used it extensively and it works very well.
I think these libraries (cdb, bsddb3, sqlite) has a jython implementation in https://code.google.com/p/django-jython/ check it out

Can I extend Jenkins with Jython/Python

Is it possible to write Jenkins plugins using Python (via Jython), and if so, where would I get started? I don't know Java (and don't have a real interest in learning it at the moment) so being able to use Python would be nice for me.
There's no docs on that. Frankly your problem with be you must understand how the interfaces and extensions map from java to jython to write it.
Here are links on writing Jenkins plugins
Let's you embed either Jython or Python scripts and run them from a plugin If all you nee to do is run a simple script, that be what you need.
If your goal is to execute Jython code within Jenkins, you may want to have a look at the Jython Plugin.
Starting version 1.6, you can actually install Jython packages (say if you have your own library you'd like to use), and it'll automatically sync up the packages across all Jenkins slaves.
Since Jython can produce Java classes I see no reason why this should not work.
Note that you will need to learn at least some things about Java (mostly the platform, not necessarily the language) in order to be able to write plugins for Jenkins.

Categories