Access a Java object at runtime in Jep-embedded Python

794 views
Skip to first unread message

Tongfei Chen

unread,
Mar 15, 2017, 1:53:26 AM3/15/17
to Jep Project
Suppose I have a Jep instance running in my Java main method.
I'd like to pass an object (not a class) from the JVM side to the Python side, so in Python I can call the methods of this object dynamically. Is this possible?


Ben Steffensmeier

unread,
Mar 16, 2017, 3:25:59 PM3/16/17
to Jep Project
Yes, Jep lets you use java objects easily from python, here is a simple example:

try (Jep jep = new Jep()){
    Object javaObject = new Object();
    jep.set("javaObject", javaObject);
    jep.eval("print(javaObject.toString())");
}



On Wed, Mar 15, 2017 at 12:53 AM, Tongfei Chen <cton...@gmail.com> wrote:
Suppose I have a Jep instance running in my Java main method.
I'd like to pass an object (not a class) from the JVM side to the Python side, so in Python I can call the methods of this object dynamically. Is this possible?


--
You received this message because you are subscribed to the Google Groups "Jep Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jep-project+unsubscribe@googlegroups.com.
To post to this group, send email to jep-p...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jep-project/28a72662-5f7f-4bac-af8f-a3478d10bd9f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tongfei Chen

unread,
Mar 18, 2017, 9:18:17 PM3/18/17
to Jep Project
Thanks a lot !


On Thursday, March 16, 2017 at 3:25:59 PM UTC-4, Ben Steffensmeier wrote:
Yes, Jep lets you use java objects easily from python, here is a simple example:

try (Jep jep = new Jep()){
    Object javaObject = new Object();
    jep.set("javaObject", javaObject);
    jep.eval("print(javaObject.toString())");
}


On Wed, Mar 15, 2017 at 12:53 AM, Tongfei Chen <cton...@gmail.com> wrote:
Suppose I have a Jep instance running in my Java main method.
I'd like to pass an object (not a class) from the JVM side to the Python side, so in Python I can call the methods of this object dynamically. Is this possible?


--
You received this message because you are subscribed to the Google Groups "Jep Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jep-project...@googlegroups.com.
Message has been deleted
Message has been deleted
Message has been deleted

Nathan Jensen

unread,
May 18, 2018, 11:38:43 AM5/18/18
to Jep Project
Python imports are weird (in my opinion).  Back a few messages, you got an ImportError on "import java.util.ArrayList".  That doesn't work but a similar statement of "from java.util import ArrayList" does work.  You can notice the same behavior with other python packages, for example "import numpy.ndarray" fails but "from numpy import ndarray" succeeds.  You can play with these imports with the jep command line script, though it will use the default ClassEnquirer (though that will include classes on the classpath).

Java packages with io are a mess because there is a Python module named io.  https://github.com/ninia/jep/issues/29

What does your import statement look like?  If you're doing import io.knowscieng.SomeClass instead of from io.knowscieng import SomeClass that may explain the problem.




On Fri, May 18, 2018 at 5:35 AM, SemanticBeeng <semanticbeen...@gmail.com> wrote:

As per https://groups.google.com/d/msg/jep-project/uuMVFAKhIY0/vih6VDceCQAJ I implemented `ClassEnquirer`

```
    val ci = ClassList.getInstance()

    new JepConfig()
...
      /**
        * A way to be able to execute Python code embedded in the Knowscieng code base (se python folders)
        * #resource
        */
      .addIncludePaths(PythonPaths.includePaths: _*)
      .setClassEnquirer(new ClassEnquirer {
        override def isJavaPackage(name: String): Boolean = {
          if (ci.isJavaPackage(name)) {
            logger.info(s"isJavaPackage($name) = true")
            true
          }
          else if (name.startsWith("java.") || name.startsWith("io.knowscieng.")) {
           logger.info(s"isJavaPackage($name) = true")
            true
          }
          else {
           logger.info(s"isJavaPackage($name) = false")
            false
          }
        }

```
Now I am getting 

```
18/05/18 03:26:13 [jep-single-thread-19] INFO orchestration: isJavaPackage(y_module.data_layer) = false
 18/05/18 03:26:13 [jep-single-thread-19] INFO orchestration: isJavaPackage(y_module.data_layer.module) = false
 18/05/18 03:26:13 [jep-single-thread-19] INFO orchestration: isJavaPackage(signal) = false
 18/05/18 03:26:13 [jep-single-thread-19] INFO orchestration: isJavaPackage(_signal) = false
 18/05/18 03:26:13 [jep-single-thread-19] INFO orchestration: isJavaPackage(enum) = false
 18/05/18 03:26:13 [jep-single-thread-19] INFO orchestration: isJavaPackage(logging) = false
 18/05/18 03:26:13 [jep-single-thread-19] INFO orchestration: isJavaPackage(time) = false
 18/05/18 03:26:13 [jep-single-thread-19] INFO orchestration: isJavaPackage(traceback) = false
 18/05/18 03:26:13 [jep-single-thread-19] INFO orchestration: isJavaPackage(linecache) = false
 18/05/18 03:26:13 [jep-single-thread-19] INFO orchestration: isJavaPackage(tokenize) = false
 18/05/18 03:26:13 [jep-single-thread-19] INFO orchestration: isJavaPackage(re) = false
 18/05/18 03:26:13 [jep-single-thread-19] INFO orchestration: isJavaPackage(sre_compile) = false
 18/05/18 03:26:13 [jep-single-thread-19] INFO orchestration: isJavaPackage(_sre) = false
 18/05/18 03:26:13 [jep-single-thread-19] INFO orchestration: isJavaPackage(sre_parse) = false
 18/05/18 03:26:13 [jep-single-thread-19] INFO orchestration: isJavaPackage(sre_constants) = false
 18/05/18 03:26:13 [jep-single-thread-19] INFO orchestration: isJavaPackage(copyreg) = false
 18/05/18 03:26:13 [jep-single-thread-19] INFO orchestration: isJavaPackage(token) = false
 18/05/18 03:26:13 [jep-single-thread-19] INFO orchestration: isJavaPackage(warnings) = false
 18/05/18 03:26:13 [jep-single-thread-19] INFO orchestration: isJavaPackage(string) = false
 18/05/18 03:26:13 [jep-single-thread-19] INFO orchestration: isJavaPackage(_string) = false
 18/05/18 03:26:13 [jep-single-thread-19] INFO orchestration: isJavaPackage(threading) = false
 18/05/18 03:26:13 [jep-single-thread-19] INFO orchestration: isJavaPackage(atexit) = false
 18/05/18 03:26:13 [jep-single-thread-19] INFO orchestration: isJavaPackage(pandas) = false
 18/05/18 03:26:13 [jep-single-thread-19] INFO orchestration: isJavaPackage(pyspark) = false
 18/05/18 03:26:13 [jep-single-thread-19] ERROR data_layer: Error initModule(data_layer) <class 'ModuleNotFoundError'>: No module named 'io.knowscieng'; 'io' is not a package  In = 303102866, using List(/projects/knowscieng.io/.../src/main/python)

```
The import from the python code is for classes from "io.knowscieng".

But the inquirer is not even asked.
So seems to be confused by the includePaths?

Thanks
Nick


On Friday, May 18, 2018 at 6:09:41 AM UTC-4, SemanticBeeng wrote:
Hi Ben,

Another question in this context, please.

Have a Scala program calling a python script it works fine.
Now need to pass more custom Scala objects to the script running embedded .

Passing a java map works.

But trying to "import java.util.ArrayList" in Python gives " <class 'ModuleNotFoundError'>: No module named 'java.util.ArrayList' "

Have examples where starting a python script (not embeeded in Je) can import and call java objects.
But how to do same from Python running embedded

What I really want is to pass custom Scala objects to embedded Python but also import same classes from Python.

But seeing that passing java maps works without imports I am wondering what is the role of the import.

Is the solution around setting class loaders in JepConfig?

Many thanks in advance,
Nick


On Thursday, March 16, 2017 at 3:25:59 PM UTC-4, Ben Steffensmeier wrote:
Yes, Jep lets you use java objects easily from python, here is a simple example:

try (Jep jep = new Jep()){
    Object javaObject = new Object();
    jep.set("javaObject", javaObject);
    jep.eval("print(javaObject.toString())");
}


On Wed, Mar 15, 2017 at 12:53 AM, Tongfei Chen <cton...@gmail.com> wrote:
Suppose I have a Jep instance running in my Java main method.
I'd like to pass an object (not a class) from the JVM side to the Python side, so in Python I can call the methods of this object dynamically. Is this possible?


--
You received this message because you are subscribed to the Google Groups "Jep Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jep-project...@googlegroups.com.

To post to this group, send email to jep-p...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jep-project/28a72662-5f7f-4bac-af8f-a3478d10bd9f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Jep Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jep-project+unsubscribe@googlegroups.com.
To post to this group, send email to jep-p...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages