我使用org.python.util.PythonInterpreter类在java中执行python代码。请在下面找到我的代码片段。
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);以上代码的问题在于,对于相同的脚本,有时我的“consoleOutput”为空。我找不出这个问题。对于运行上述代码1000次,至少4次我得到空输出。
另一方面,如果我使用默认构造函数(如下面所示),它就能正常工作。
PythonInterpreter pythonInterpreter = new PythonInterpreter();发布于 2017-11-08 19:31:25
深入了解我发现的问题,将属性python.site.import设置为false会触发这个问题。此问题发生在Jython独立版本2.7.0中。更新到2017年6月发布的独立jar(2.7.1)解决了这个问题。
https://stackoverflow.com/questions/46300191
复制相似问题