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

Importing tensorflow in python script imports the Java Tensorflow library and not python's #377

Closed
priyadarshi9 opened this issue Feb 23, 2022 · 2 comments

Comments

@priyadarshi9
Copy link

Describe the problem
I am trying to run a python script from java with the below import statement.

import tensorflow as tf

When I run this script using jep, it imports the java tensorflow library ( mavan's tensorflow library is a dependency in the java project) and not python's tensorflow library. Then, the below import fails.

from tensorflow import keras

Exception:
jep.JepException: <class 'ImportError'>: tensorflow.keras
at /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/jep/java_import_hook.getattr(java_import_hook.py:57)
at /Users/kpriyadarsh/amelia/ameliav3/ameliav3/amelia-nlp-api-impl/src/main/java/net/ipsoft/amelia/nlp/ml/wrapper/bert_model_trainer.(bert_model_trainer.py:16)
at .(:1)
at jep.Jep.exec(Native Method)
at jep.Jep.exec(Jep.java:339)
at com.roberta.BertTrainer.train(BertTrainer.java:55)
at com.roberta.TrainerRunner.main(TrainerRunner.java:52)
Caused by: java.lang.ClassNotFoundException: tensorflow.keras
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 4 more

How can I stop jep from trying to load the java tensorflow dependency and make it import the python's tensorflow library. Any help would be appreciated.
Environment (please complete the following information):

  • OS Platform, Distribution, and Version: MacOS BigSur 11.1
  • Python Distribution and Version:3.7.1
  • Java Distribution and Version:Amazon Coretto 11
  • Jep Version:4.0.1
  • Python packages used (e.g. numpy, pandas, tensorflow):tensorflow
@bsteffensmeier
Copy link
Member

The ClassEnquirer is responsible for determining which java packages can be imported into python. You will need to set a custom ClassEnquirer on your JepConfig that returns false when tensorflow is passed to isJavaPackage, You could try the NamingConventionClassEnquirer that comes with jep but if that doesn't work for you you may need to implement the interface yourself to get the desired behavior.

@priyadarshi9
Copy link
Author

Thanks, I implemented the ClassEnquirer and it works now. Thank you very much!

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