ninia / jep

Embed Python in Java
Other
1.31k stars 149 forks source link

Jep.setJepLibraryPath(...) called after initializing python interpreter. #470

Closed ktts1212 closed 1 year ago

ktts1212 commented 1 year ago

I try to use jep in the Springboot project but it can only be used once, and then it reports an error: Jep.setJepLibraryPath(...) called after initializing python interpreter. . I tried using the close method in the Interpreter hoping to close the Interpreter object, but it didn't work, the close method doesn't seem to be working.

jep version is 4.1.1 jdk version is 1.8

`public class Jeptest{

public void sss(){
    String[] array_list= {"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1"};

    String result=null;
    JepConfig config=new JepConfig();
    config.addIncludePaths("D:\\PycharmProjects\\C4.5cal");
    MainInterpreter.setJepLibraryPath("D:\\Python\\Lib\\site-packages\\jep\\jep.dll");

    try(Interpreter interp=new SubInterpreter(config)) {
        interp.eval("from main import getResult,my_list,my_str");
        for (int i=0;i<array_list.length;i++){
            interp.set("my_str",array_list[i]);
            interp.exec("my_list.append(my_str)");
        }
        interp.eval("result=getResult(my_list)");
        //jep.python.PyObject result=interp.getValue("result",jep.python.PyObject.class);
        result=(String)interp.getValue("result");
        System.out.println(interp);
        interp.close();

        System.out.println(interp);
    }catch (Exception e){
        e.printStackTrace();
    }

}`
bsteffensmeier commented 1 year ago

The jep native library is only loaded once per process so setJepLibraryPath must be called before any interpreter is loaded and cannot be called again after an interpreter is created. The close method will end a specific interpreter but does not reset configuration that applies to the entire process.

ktts1212 commented 1 year ago

I have know what I should do.Thanks for your help and your product.