代码之家  ›  专栏  ›  技术社区  ›  htopiwala

PythonInterpreter exec函数返回空输出流

  •  0
  • htopiwala  · 技术社区  · 8 年前

    我正在使用org。蟒蛇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();
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   htopiwala    8 年前

    python.site.import false 引发此问题。这个问题发生在Jython单机版2.7.0中。更新到2017年6月发布的独立jar(2.7.1)解决了这个问题。