4

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 2.7.1. The following code throws error at the execfile call (the code is able to get the Python file as input stream correctly):

ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayOutputStream err = new ByteArrayOutputStream();
String[] result = new String[2];
Properties preprops = System.getProperties();

Properties props = new Properties();
//props.put("python.home", "c:/Python27");
//props.put("python.path", "c:/Python27/Lib");
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());

    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 json

I assume that it is probably happening because I am unable to set the property "python.home" or "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). If I set the "python.home" or "python.path" property to my local Python installation path, the code works just fine.

Appreciate your help in solving this. A sample application is available at https://github.com/dchucks/jython to help reproduce the error.

Dchucks
  • 1,189
  • 5
  • 22
  • 48
  • Is this what you need? https://www.stefaanlippens.net/jython_and_pythonpath/ – moilejter Jul 30 '18 at 07:10
  • Hi @moilejter, sorry but no. My issue is that my application is to be deployed on a PaaS platform where I wont know if Python is pre-installed, and if yes at what path. If Jython expects that Pythin should be pre-installed on the machine or we are able to provide an absolute path of the Jython lib (which is inside the application jar) then I am in trouble. – Dchucks Jul 31 '18 at 12:53
  • check here http://bugs.jython.org/issue2355 "they encountered similar issue with py.java" – recursion Aug 03 '18 at 06:33

0 Answers0