From: Debashish <deb...@gm...> - 2018-08-11 19:16:52
|
Hi, I am trying to call a Python Script from a Spring Boot applications. There are two issues I am facing: (1) The Jython Standalone JAR is not detected and I am forced to supply the local install path as "python.home" to make it work. (2) Once I initialize the *PythonInterpreter* class, it seems it caches the argv I supply to it as for the subsequent invocation it uses the same argv values. I do call the close() method to do cleanup, but it doesn't help :( Properties preprops = System.getProperties(); Properties props = new Properties(); props.put("python.home", "C:\\Dev\\Python27"); props.put("python.console.encoding", "UTF-8"); props.put("python.security.respectJavaAccessibility", "false"); props.put("python.import.site", "false"); PythonInterpreter.initialize(preprops, props, arguments); PythonInterpreter pyInterpreter = new PythonInterpreter(); try { resource = new ClassPathResource("mypyscript.py"); out = new ByteArrayOutputStream(); err = new ByteArrayOutputStream(); pyInterpreter.setOut(out); pyInterpreter.setErr(err); pyInterpreter.execfile(resource.getInputStream()); result[0] = out.toString(); // reading the output result[1] = err.toString(); // reading any error } catch (Exception e) { throw new Exception(e); } finally { try { if (out != null) out.close(); if (err != null) err.close(); pyInterpreter.close(); } catch (IOException e) { e.printStackTrace(); } } Please help. Thanks, Deb |