I am trying to invoke a Python script from a Spring Boot application which is bundled as a JAR. I added Maven dependency for Jython Standalone JAR. The following code throws error at the execfile call (the code is able to get the Python file as input stream correctly):
Appreciate your help in solving this.
//Java Code, calling Python script ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream err = new ByteArrayOutputStream(); String[] result = new String[2]; Properties preprops = System.getProperties(); Properties props = new Properties(); //props.put("python.path", "?"); props.put("python.console.encoding", "UTF-8"); props.put("python.security.respectJavaAccessibility", "false"); props.put("python.import.site", "false"); PythonInterpreter.initialize(preprops, props, new String[0]); PythonInterpreter pyInterpreter = new PythonInterpreter(); try { Resource resource = new ClassPathResource("myPythonFile.py"); pyInterpreter.set("args", "a string argument"); pyInterpreter.setOut(out); pyInterpreter.setErr(err); pyInterpreter.execfile(resource.getInputStream()); //this line throws error result[0] = out.toString();// reading the output result[1] = err.toString();// reading the error } catch (Exception e) { logger.error("Error in code: ", e); } finally { try { if (out != null) out.close(); if (err != null) err.close(); pyInterpreter.close(); } catch (IOException e) { e.printStackTrace(); } }Error thrown:
org.python.core.PyException: null at org.python.core.Py.ImportError(Py.java:334) ~[jython-standalone-2.7.1.jar!/:na] at org.python.core.imp.import_first(imp.java:879) ~[jython-standalone-2.7.1.jar!/:na] at org.python.core.imp.import_module_level(imp.java:964) ~[jython-standalone-2.7.1.jar!/:na] at org.python.core.imp.importName(imp.java:1057) ~[jython-standalone-2.7.1.jar!/:na] at org.python.core.ImportFunction.__call__(__builtin__.java:1280) ~[jython-standalone-2.7.1.jar!/:na] at org.python.core.PyObject.__call__(PyObject.java:450) ~[jython-standalone-2.7.1.jar!/:na] at org.python.core.__builtin__.__import__(__builtin__.java:1232) ~[jython-standalone-2.7.1.jar!/:na] at org.python.core.imp.importOne(imp.java:1076) ~[jython-standalone-2.7.1.jar!/:na] at org.python.pycode._pyx1.f$0(<iostream>:251) ~[na:na] at org.python.pycode._pyx1.call_function(<iostream>) ~[na:na] at org.python.core.PyTableCode.call(PyTableCode.java:171) ~[jython-standalone-2.7.1.jar!/:na] at org.python.core.PyCode.call(PyCode.java:18) ~[jython-standalone-2.7.1.jar!/:na] at org.python.core.Py.runCode(Py.java:1614) ~[jython-standalone-2.7.1.jar!/:na] at org.python.util.PythonInterpreter.execfile(PythonInterpreter.java:296) ~[jython-standalone-2.7.1.jar!/:na] at org.python.util.PythonInterpreter.execfile(PythonInterpreter.java:291) ~[jython-standalone-2.7.1.jar!/:na]My Python script is using the following imports:
import sys import argparse import re import jsonI assume that it is probably happening because I am unable to set the property "python.path", but I am not sure how could I do that (the Jython JAR is being bundled under BOOT-INF/lib by Spring Boot). I cannot possibly supply the absolute path of Pythin installation (if that is what Jython expects) as the JAr would be deployed on a PaaS where file system access is prohibited.
Appreciate your help in solving this.