Hi Menelaos.

The methods at the top of your stack trace implement the import statement. It is not CLASSPATH that is your problem, I would say, but the path that controls the search for Python modules. This is helpful:

http://www.jython.org/jythonbook/en/1.0/ModulesPackages.html#module-search-path-and-loading

I'm sometimes bitten by this in my IDE when I forget to add a JVM argument -Dpython.home= ... , or rather when Eclipse makes up a configuration without it. In that case, the standard library is missing from the search, and sys.path is just:
['__classpath__', '__pyclasspath__/']
When that happens, you can only find the built-in modules (such as sys, os, etc.), and Java runtime. (When running Jython at the command prompt, a wrapper script is invoked that defines an awful lot of things that will be missing if you just launch the JAR.) If you can't find 'datetime', that may be your problem.

If your import succeeds for standard library modules, but not for extra modules you want (I think json is not standard in 2.5.3), then you probably need to define or add the location to python.path, either in the (Jython) registry or as a JVM argument. Again, it should show up in the ultimate value of sys.path. This discusses how to do it:
http://www.jython.org/archive/22/userfaq.html#my-modules-can-not-be-found-when-imported-from-an-embedded-application

The search strategy is quite complicated because it has to be like Python and like Java at the same time.

Jeff Allen

On 08/02/2013 13:51, Menelaos Perdikeas wrote:

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)
...