ninia / jep

Embed Python in Java

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Exception in thread "main" jep.JepException: <class 'ModuleNotFoundError'>: No module named 'jep'

sifislaz opened this issue · comments

I want to use a Python library downloaded as a git submodule on my Java project, but first I want to test that I have installed properly jep to my project, so I tried to run a simple program:

public static void main(String[] args){
  try(Interpreter inter = new SharedInterpreter()){
              inter.exec("import math");
              inter.set("x",2);
              inter.exec("y = math.sqrt(x)");
              Object res = inter.getValue("y");
              System.out.println(res);
          }
    }

When I executed the code the following error occured: Exception in thread "main" jep.JepException: <class 'ModuleNotFoundError'>: No module named 'jep'
Do you know how can I overcome this problem?
Environment (please complete the following information):

  • OS Platform, Distribution, and Version: Windows 10
  • Python Distribution and Version: Python 3.10.4 (using virtual environment)
  • Java Distribution and Version: java 17 2021-09-14 LTS with Maven
  • Jep Version: 4.1.1 (Downloaded with pip install jep)
  • Python packages used (e.g. numpy, pandas, tensorflow): None yet, since it is a simple test

That is unusual and I am not certain what would cause that. I recommend checking to make sure that jep is installed into the virtual environment and that the virtual environment is activated before the java application is started.

How to activate the virtual environment? i have load the jep.dll into the path and with function MainInterpreter.setJepLibraryPath(), what else can i do?

Hey, for activation you source /bin/activate - probably before you start your java process. By default virtualenv does not put the python installation's site-packages onto the path. So if you start your plain python in virtualenv, jep will also not be found (try to 'import jep').
So as bsteffensmeier mentioned, make sure to have jep installed / on the path. You can also initialize virtualenv with "--system-site-packages", like this you should automatically be able to see jep (if it is installed in your python installation).

Alternatively you should be able to extend the python path manually with JepConfig.addIncludePaths and SharedInterpreter.setConfig(JepConfig).

Note thtat this is additional to your already mentioned MainInterpreter.setJepLibraryPath. One is for finding python modules in python, the other for finding the jep C-library in java.

I think, if you try --system-site-packages, both are not needed.

It does help, thank you very much!

I am having this issue as well. I've tried installing jep as a system wide package, as well as in a virtual environment. However, since I am getting a different error when jep is not installed (similar to #517 ) I suspect there's something else wrong.

Could this be because I am running my java code in a non-main thread?

I tried running this as the very first thing in my application, and is still getting the error:

Exception in thread "main" jep.JepException: <class 'ModuleNotFoundError'>: No module named 'jep'
        at <string>.<module>(<string>:1)
        at jep.Jep.eval(Native Method)
        at jep.Jep.eval(Jep.java:326)
        at jep.Jep.configureInterpreter(Jep.java:191)
        at jep.SharedInterpreter.configureInterpreter(SharedInterpreter.java:68)
        at jep.Jep.<init>(Jep.java:170)
        at jep.SharedInterpreter.<init>(SharedInterpreter.java:60)
        at dk.gtz.graphedit.Main.main(Main.java:26)

I would like to note that the error message is suspiciously similar to a regular bad module import message from python.

I am using jep v4.2.0

@sillydan1 Do you have multiple python installation on your system. That can occur if java is loading libpython from one python installation while jep is installed in a different python installation.

I would like to note that the error message is suspiciously similar to a regular bad module import message from python.

It is a regular import error message, it has just been converted to a java exception. You should be able to troubleshoot this like any other module install error and make sure that the jep python library is installed correctly in your python.