PythonInterpreter exec 函数 return 空输出流
PythonInterpreter exec function return empty output stream
我正在使用 org.python.util.PythonInterpreter class 来执行 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);
上面代码的问题是 same 脚本有时我得到 'consoleOutput' 空。我无法弄清楚问题所在。对于 运行 上面的代码 1000 次,至少有 4 次我得到空输出。
另一方面,如果我使用如下所示的默认构造函数,它工作得很好
PythonInterpreter pythonInterpreter = new PythonInterpreter();
深入研究我发现的问题,将 属性 python.site.import
设置为 false
会触发此问题。此问题出现在 Jython 独立版本 2.7.0 中。更新到 2017 年 6 月发布的独立 jar(2.7.1) 可以修复此问题。
我正在使用 org.python.util.PythonInterpreter class 来执行 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);
上面代码的问题是 same 脚本有时我得到 'consoleOutput' 空。我无法弄清楚问题所在。对于 运行 上面的代码 1000 次,至少有 4 次我得到空输出。
另一方面,如果我使用如下所示的默认构造函数,它工作得很好
PythonInterpreter pythonInterpreter = new PythonInterpreter();
深入研究我发现的问题,将 属性 python.site.import
设置为 false
会触发此问题。此问题出现在 Jython 独立版本 2.7.0 中。更新到 2017 年 6 月发布的独立 jar(2.7.1) 可以修复此问题。