简体   繁体   中英

PythonInterpreter exec function return empty output stream

I'm using org.python.util.PythonInterpreter class to execute python code in java. Please find below the snippet of of my code.

PythonInterpreter pythonInterpreter = new PythonInterpreter(null, new PySystemState());

ByteArrayOutputStream outStream = new ByteArrayOutputStream(16384);

pythonInterpreter.setOut(outStream);
pythonInterpreter.setErr(outStream);

// execute the code
pythonInterpreter.exec(script);

String consoleOutput = outStream.toString();
outStream.flush();

System.out.println("Console output :- "+consoleOutput);

The problem with the above code is for the same script sometimes I get 'consoleOutput' empty. I'm not able to figure out the problem. For running the above code 1000 times, at least 4 times I get empty output.

On the other hand if I use the default constructor as shown below it works just fine

PythonInterpreter pythonInterpreter = new PythonInterpreter();

Digging more into the problem I found, setting the property python.site.import to false triggers this problem. This issue occurs in Jython standalone version 2.7.0. Updating to the June 2017 release of the standalone jar(2.7.1) fixes this issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM