ninia / jep

Embed Python in Java
Other
1.3k stars 147 forks source link

How can I activate a different conda environment at runtime? #408

Closed fguillotpro closed 2 years ago

fguillotpro commented 2 years ago

Hello! I am using a "hardcoded" conda environment set using MainInterpreter.setJepLibraryPath(), it is working fine (still need to set LD_PRELOAD manually for numpy to work but I see that it is being resolved). I use SharedInterpreter:

public void init() {
    MainInterpreter.setJepLibraryPath("/opt/conda/envs/<conda_env_name>/lib/python3.8/site-packages/jep/libjep.so");
}

public Object executeService() throws JepException {
    try (Interpreter interp = new SharedInterpreter()) {
       // do stuff like interp.exec(code) and interp.invoke(function, params)
    }
}

Now I would like to be able to dynamically specify a different environment at runtime (equivalent of "source activate some_env") to be used by the executeService method. Instead of relying on my init(), maybe I could use a pool of interpreters initialized with the different environments. With my current understanding of JEP, I am not sure if it can be done within the same JVM. If yes, how do I proceed? Thanks.

Environment:

bsteffensmeier commented 2 years ago

With my current understanding of JEP, I am not sure if it can be done within the same JVM.

That is correct, once jep is loaded it will continue to use the same Python environment until the JVM terminates and there is no way to change that.

fguillotpro commented 2 years ago

With my current understanding of JEP, I am not sure if it can be done within the same JVM.

That is correct, once jep is loaded it will continue to use the same Python environment until the JVM terminates and there is no way to change that.

Ok thanks.