3

I am using the Jython 2.5.3 PythonInterpreter class to evaluate some simple scripts but when I need to import any non-core modules I get an exception. Do I have to add some jython library jars in the CLASSPATH?

Narrow-down code that demonstrates 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 the above with only jython-2.5.3.jar in the 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)

1 Answer 1

4

You seem to be using the Jython jar file that doesn't include the bundled Python files (the standard library in the /Lib folder). I believe it should work if you use the standalone jar instead. See

Sign up to request clarification or add additional context in comments.

3 Comments

I can confirm that with the standalone jar in the runtime classpath the module 'datetime' is now found (whereas previously, it was not). 'import json' however still yields the same failure in both cases (either with jython-standalone-2.5.3.jar or with jython-2.5.3.jar) in the runtime classpath.
The json module was added in Python 2.6. It is not available in Jython 2.5.3. You may want to try Jython 2.7beta1, which includes json. See jython.org/downloads.html.
I just had the issue. Added jython-standalone 2.7.0 released version in my pom.xml. Works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.