Cannot see print and println statements in Eclipse using JEP/Pydev

431 Views Asked by At

I'm learning JEP and PyDev plugin eclipse and new to Python.

I cannot see my python print and java println statements on Eclipse console tab.

As I'm just trying things out I create a simple python script by creating a new PyDev module and it just has one line (greetings.py):

print("Hello from python");

When I run this I see it in the console when I run it both the PyDev and Jave EE perspective.

Next as the intent of this exercise is to look into JEP to see if it's adequate for my project so I created another Java project with this code:

package my.sand.box;

import jep.Interpreter;
import jep.Jep;
import jep.JepException;
import jep.SharedInterpreter;

public class JepTest {

    public static void main(String[] args) throws JepException {
        // TODO Auto-generated method stub
        System.out.println("hey");

        try (Interpreter interp = new SharedInterpreter()) {
            //interp.exec("import example_package");
            // any of the following work, these are just pseudo-examples

            interp.runScript("full/path/to/greetings.py");
            interp.eval("import sys");
            interp.eval("s = 'Hello World'");
            interp.eval("print s");
             String java_string = interp.getValue("s").toString();
             System.out.println("Java String:" + java_string);
    }
    }

}

I don't see anyting on the console. Not even the java println statements.

I also recreated both projects in a new workspace and could see the output. What's different between both workspaces is that in the one that's not workign I have other java projects and pydev projects open. Would appreciate any advice.

2

There are 2 best solutions below

0
On

I've faced a similar issue working with Jep before, the trick is you need to redirect Python's output stream in your IDE by calling the correct method.

Take a look at https://github.com/ninia/jep/issues/298

0
On

As Klodovsky mentioned, you need to redirect Python's output to the stream used by the IDE. I've adapted your example to do this. The key line is the call to SharedInterpreter.setConfig:

package my.sand.box;

import jep.Interpreter;
import jep.JepConfig;
import jep.JepException;
import jep.SharedInterpreter;

public class JepTest {

    public static void main(String[] args) throws JepException {
        System.out.println("hey");

        // Eclipse doesn't use stdout & stderr, so use the streams from Java.
        SharedInterpreter.setConfig(new JepConfig()
                .redirectStdErr(System.err)
                .redirectStdout(System.out));

        try (Interpreter interp = new SharedInterpreter()) {
            // interp.exec("import example_package");
            // any of the following work, these are just pseudo-examples

            // Uncomment if you've created a greetings.py script.
            // interp.runScript("full/path/to/greetings.py");
            interp.eval("import sys");
            interp.eval("s = 'Hello World'");
            interp.eval("print(s)");
            String java_string = interp.getValue("s").toString();
            System.out.println("Java String:" + java_string);
        }
    }
}