Python Forum
Jython code throws ImportError when invoked from a Java jar
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Jython code throws ImportError when invoked from a Java jar
#1
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):

//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 json
I 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.
Reply
#2
Could you generate a minimal Github project that reproduces your issue? I'm intrigued by this and would love to dig into it, but full instructions for reproducing it would be great!
Reply
#3
Thanks a lot for your reply. I added the code at https://github.com/dchucks/jython. To run the code:
mvn clean install
java -jar target/jython-0.0.1-SNAPSHOT.jar
The error I am getting is now more clear, than it was on my original project. The error talks about inability to import the modules, but when I see within the Jython JAR that is bundled within the application JAR, I can see that libraries such as argparse, re, json are present there.
Reply
#4
Ok so my best guess is that argparse can't be used with Jython. Jython doesn't support Python 2.7 or newer, which is what is required for argparse.
Reply
#5
Thanks. Actually the code throws same error even for other modules, with following imports also it says "ImportError: No module named os":
import os
import re
import json
I had two doubts:
  1. When I use Jython JAR in my application does the target system need to have Python installed? I am assuming not because the standalone jar has the Python runtime and libraries and moreover I am using the "standalone" Jython JAR.
  2. Does Jython expects the PythonInterpreter to know the "python.path" or "python.home"? Since Jython JAR is bundled in the Java application JAR there is no way for me to provide an absolute path of the Jython JAR to PythonInterpreter.
Reply
#6
I was mistaken - I hadn't looked at Jython in a few years, and I thought development ceased quite a while ago, but looking into it, that's not true at all. Weird that when I ran your code, it choked on argparse but had gotten past sys successfully. That said, I'm at a loss.

(Aug-01-2018, 05:26 PM)dchucks Wrote: When I use Jython JAR in my application does the target system need to have Python installed?
I'm pretty confident not, since it's a whole separate Python implementation.

Beyond this, I'd suggest you try to look into something Jython specific. Since Jython appears to be in active development now there should be some community you can get help from (even if it's just the devs), but unless someone else here wants to really dive in, I don't think we're going to be very helpful.
Reply
#7
Thanks for all your help. Incidentally, yesterday I set the "python.home" property to my local Python 2.7 installation folder and the code worked just fine, which means that the issue is that PythonInterpreter is unable to get information about the Jython JAR bundled and when we point it to any other Python install path it uses those to execute the code. So the real challenge for me now is how to provide the Jython JAR path to my applications. Thanks again, will try to reach out to Jython DEV as you suggested.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pymysql: insert query throws error wardancer84 12 4,584 Jan-28-2022, 06:48 AM
Last Post: wardancer84
  issue with Tabula-py, pyinstaller and java maurom82 2 3,176 Feb-19-2021, 04:32 PM
Last Post: buran
  pyarrow throws oserror winerror 193 1 is not a valid win32 application aupres 2 3,782 Oct-21-2020, 01:04 AM
Last Post: aupres
  Function throws error but then works? Milfredo 10 3,781 Sep-12-2020, 05:16 AM
Last Post: Milfredo
  python/winrt Bluetooth.GenericAttributProfile CreateAsync(GUID) method throws Not Imp pbvinoth 0 2,136 Jul-08-2020, 04:27 AM
Last Post: pbvinoth
  Jython macro which uses PythonInterpreter and Redis script gives an error rkanumola 2 2,248 Oct-30-2019, 07:37 AM
Last Post: rkanumola
  How to run python code in Java using remote SSH without py file using streams in Java varanasipavankumar 0 2,535 Apr-07-2019, 06:13 PM
Last Post: varanasipavankumar
  running python script from shell invoked with os.system("x-terminal-emulator -e /bin/ markhaus 2 3,074 Feb-21-2019, 11:55 PM
Last Post: markhaus
  Can't find error in code but Python throws exception Sandwich_masterX 3 2,941 Oct-09-2018, 01:38 AM
Last Post: ichabod801
  phython language java integration jammytcs123123 1 2,290 Jul-04-2018, 03:13 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020