Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jep.JepException: <class 'ImportError'>: math.log #541

Open
mwxiaomao opened this issue Jul 4, 2024 · 2 comments
Open

jep.JepException: <class 'ImportError'>: math.log #541

mwxiaomao opened this issue Jul 4, 2024 · 2 comments

Comments

@mwxiaomao
Copy link

Describe the problem
A clear and concise description of what the problem is.

Environment (please complete the following information):

  • OS Platform, Distribution, and Version: mac 10.12
  • Python Distribution and Version: 3.7.9
  • Java Distribution and Version: 1.8.0_251-b08
  • Jep Version: 4.2.0
  • Python packages used (e.g. numpy, pandas, tensorflow): No

Java code:
try (Jep jep = new SubInterpreter()) {
jep.eval("import math");
jep.eval("result = math.log(10)");
double result = jep.getValue("result", Double.class);
System.out.println("Natural logarithm of 10 is: " + result);
} catch (JepException e) {
e.printStackTrace();
}

jep.JepException: <class 'ImportError'>: math.log
at /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/jep/java_import_hook.getattr(java_import_hook.py:57)
at .(:1)
at jep.Jep.eval(Native Method)
at jep.Jep.eval(Jep.java:326)
at com.my.cloud.JepTest.main(JepTest.java:30)
Caused by: java.lang.ClassNotFoundException: math.log
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 3 more

@ndjensen
Copy link
Member

ndjensen commented Jul 4, 2024

The ClassNotFoundException in the stacktrace indicates it's trying to import math.log as if it was a Java class. You presumably have a Java package on your classpath starting with math. Jep uses a ClassEnquirer to decide if it should attempt an import from Java or Python. You will need to implement a custom ClassEnquirer that indicates correctly if the package should be imported from Java or Python. The default ClassEnquirer, ClassList, takes all the Java packages on the classpath and indicates those should be imported from Java, which is why I believe you have a package starting with math on your classpath.

For related issues, discussion, and examples, see #173, #377, #466.

@mwxiaomao
Copy link
Author

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants