Jython :: what modules are available for PythonInterpreter and how to add more

I am using Jython 2.5.3 class PythonInterpreter

to evaluate some simple scripts, but when I need to import any non-core modules I get an exception. Should I add java java libraries to CLASSPATH library?

Narrow code demonstrating the problem:

import org.python.core.*;
import org.python.util.PythonInterpreter;

public class JythonTest {

    public static void main(String args[]) throws Exception {
        String scriptA = "import json"; // "import datetime" fails as well
        PythonInterpreter pi = new PythonInterpreter();
        PyCode code = pi.compile(scriptA);
        PyObject result = pi.eval(code);
    }
}

      

Running above with jython-2.5.3.jar in CLASSPATH fails with the following trace:

 [java] ImportError: No module named json
 [java] 
 [java]     at org.python.core.Py.ImportError(Py.java:304)
 [java]     at org.python.core.imp.import_first(imp.java:755)
 [java]     at org.python.core.imp.import_module_level(imp.java:837)
 [java]     at org.python.core.imp.importName(imp.java:917)
 [java]     at org.python.core.ImportFunction.__call__(__builtin__.java:1220)
 [java]     at org.python.core.PyObject.__call__(PyObject.java:357)
 [java]     at org.python.core.__builtin__.__import__(__builtin__.java:1173)
 [java]     at org.python.core.imp.importOne(imp.java:936)
 [java]     at org.python.pycode._pyx0.f$0(<script>:2)
 [java]     at org.python.pycode._pyx0.call_function(<script>)
 [java]     at org.python.core.PyTableCode.call(PyTableCode.java:165)
 [java]     at org.python.core.PyCode.call(PyCode.java:18)
 [java]     at org.python.core.Py.runCode(Py.java:1275)
 [java]     at org.python.core.__builtin__.eval(__builtin__.java:484)
 [java]     at org.python.core.__builtin__.eval(__builtin__.java:488)
 [java]     at org.python.util.PythonInterpreter.eval(PythonInterpreter.java:198)
 [java]     at JythonTest.main(JythonTest.java:10)

      

+3


source to share


1 answer


You seem to be using a Jython jar file which does not include the associated Python files (standard library in folder /Lib

). I believe it should work if using an offline jar instead. Cm.



+4


source







All Articles