Calling python from java using jython and passing parameters

1.6k views Asked by At

I want to call python from the java code and pass the parameters to function in python. I have encapsulated a function in python. But the thing is I have also imported third party library sklearn in the python program. When I call a test program and pass the value to python, I am able to run the code. But when I tried using third party library such as sklearn and import in the python code, I got following error.

ImportError: No module named sklearn

I initialized object for the class PythonInterpreter and then using the following command, I included the path for the sklearn

this.interpreter.exec("import sys");
this.interpreter.exec("sys.path.append(\"/usr/local/lib/python2.7/dist-packages/sklearn\")");

Can anyone tell how to fix this problem? How to include third party library in the jython code and run the program?

1

There are 1 answers

0
stewori On

As sklearn depends on native CPython extensions, it currently won't work with Jython. However, keep an eye on JyNI (www.jyni.org), which will vastly improve this issue, but is still in an early state. Until JyNI is sufficiently mature, you can use one of the following projects:

They work by embedding the CPython interpreter and don't integrate with Jython. Still, if you depend on things like scikit or numpy, these are currently the only workable approaches to use your Python-code from Java.