Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reportlab in Jython "import error"

I am using jdk 1.8 and Jython 2.7.0.jar to execute my python Code. But It seems Jython does not support Python's 2.7 feature 'reportlab' module.

My Java Code :

package test;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class Test{
        public Test()  
        {
         System.out.println("Done!");
        engine.eval("import sys");
        engine.eval("print sys");
        engine.eval("import reportlab");
        engine.eval("print reportlab");
        System.out.println("Done Here");
    } 
    catch (ScriptException ex) 
    {
        Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        System.out.println(ex);
    }
}
public static void main(String args[])
{
    Test t=new Test();
}
}

Error I got:

run:
Done!
<module 'sys' (built-in)>
javax.script.ScriptException: ImportError: No module named reportlab in    <script> at line number 1
Dec 28, 2015 8:57:52 PM test.Test <init>
SEVERE: null
javax.script.ScriptException: ImportError: No module named reportlab in    <script> at line number 1
    at      org.python.jsr223.PyScriptEngine.scriptException(PyScriptEngine.java:202)
at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:42)
at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:31)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
at test.Test.<init>(Test.java:28)
at test.Test.main(Test.java:40)
Caused by: Traceback (most recent call last):
 File "<script>", line 1, in <module>
ImportError: No module named reportlab

at org.python.core.Py.ImportError(Py.java:328)
at org.python.core.imp.import_first(imp.java:877)
at org.python.core.imp.import_module_level(imp.java:972)
at org.python.core.imp.importName(imp.java:1062)
at org.python.core.ImportFunction.__call__(__builtin__.java:1280)
at org.python.core.PyObject.__call__(PyObject.java:431)
at org.python.core.__builtin__.__import__(__builtin__.java:1232)
at org.python.core.imp.importOne(imp.java:1081)
at org.python.pycode._pyx2.f$0(<script>:1)
at org.python.pycode._pyx2.call_function(<script>)
at org.python.core.PyTableCode.call(PyTableCode.java:167)
at org.python.core.PyCode.call(PyCode.java:18)
at org.python.core.Py.runCode(Py.java:1386)
at org.python.core.__builtin__.eval(__builtin__.java:497)
at org.python.core.__builtin__.eval(__builtin__.java:501)
at org.python.util.PythonInterpreter.eval(PythonInterpreter.java:259)
at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:40)
... 4 more

BUILD SUCCESSFUL (total time: 2 seconds)

If anyone could suggest me an Alternate way of remotely executing a Python script I'd be thankful to them.

Also I have a large code base in python of which rewriting in java is not feasible in scheduled time.

like image 905
Newbee Avatar asked Apr 16 '26 12:04

Newbee


1 Answers

Jython is a completely different (and independent) Python interpreter than your system's Python. While most libraries from Python's standard library will exist in Jython, third party modules installed in your system's Python simply don't exist for the Jython version.

Jython is compatible enough that you can set a Python virtualenv with the Jython interpreter and install modules that are pure-python code (i.e. no binary modules) in that virtualenv - I don't think repotlab qualifies as such - but you might try. In a Java project setup, probably your project have its own instance of the Jython interpreter installed along other .jar files - it should feature a site-packages folder somewhere where you could try to copy pure Python third-party modules to, but again, I don't think reportlab will be jython compatible.

One option for you is to use Python itself (cPython) in an external process, and call it from Java (or Jython) using xmlrpc code - (A quickly googling didn't find me out a way to call cPython via xmlrpc from pure Java - but I know it works from Jython, if you use Python's stdlib xmlrpc as documented - https://docs.python.org/3/library/xmlrpc.html?highlight=xmlrpc

like image 133
jsbueno Avatar answered Apr 19 '26 00:04

jsbueno



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!