首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PythonInterpreter exec函数返回空输出流。

PythonInterpreter exec函数返回空输出流。
EN

Stack Overflow用户
提问于 2017-09-19 12:14:07
回答 1查看 246关注 0票数 0

我使用org.python.util.PythonInterpreter类在java中执行python代码。请在下面找到我的代码片段。

代码语言:javascript
复制
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次我得到空输出。

另一方面,如果我使用默认构造函数(如下面所示),它就能正常工作。

代码语言:javascript
复制
PythonInterpreter pythonInterpreter = new PythonInterpreter();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-08 19:31:25

深入了解我发现的问题,将属性python.site.import设置为false会触发这个问题。此问题发生在Jython独立版本2.7.0中。更新到2017年6月发布的独立jar(2.7.1)解决了这个问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46300191

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档