1

Using jython, I used a Python file in my Java code like below. package jython_connect;

import org.python.core.*;
import org.python.util.PythonInterpreter;

public class jython_test1 {

    private static PythonInterpreter interpreter;

    public static void main(String[] args) {
        interpreter = new PythonInterpreter();
        interpreter.execfile("test.py");
        PyObject str = interpreter.eval("test(7)");
        System.out.println(str);
    }

}

And in test.py include python library like numpy. An import error occurs as shown below.

ImportError: No module named numpy

I am using Jython 2.7.1, and I ran the below command but it failed.

jython -m pip install numy

How can I use the Python library in Java?

송준석
  • 991
  • 1
  • 16
  • 32
  • 2
    NumPy does not work with Jython. See https://scipy.org/scipylib/faq.html#does-numpy-scipy-work-with-jython-or-c-net, https://stackoverflow.com/q/19455100/407651. – mzjn Nov 04 '19 at 06:42
  • Can't i use any other Python library? – 송준석 Nov 04 '19 at 07:27
  • You can use most Python libraries in Jython, but not libraries that use C extensions written for CPython (such as NumPy). – mzjn Nov 04 '19 at 07:32
  • You might want to try JyNI (https://www.jyni.org/) or numjy (https://github.com/Yaqiang/numjy). See also older SO questions: https://stackoverflow.com/search?q=%5Bjython%5D+numpy+is%3Aquestion. – mzjn Nov 04 '19 at 07:35
  • Is there a list of libraries that can't be used with jython? – 송준석 Nov 04 '19 at 07:36
  • I am not aware of such a list. – mzjn Nov 04 '19 at 07:40

0 Answers0