Jython and python modules

Geo picture Geo · Jan 22, 2009 · Viewed 17.3k times · Source

I've just started using the PythonInterpreter from within my Java classes, and it works great! However, if I try to include python modules (re, HTMLParser, etc.), I'm receiving the following exception (for re):

Exception in thread "main" Traceback (innermost last):
  File "", line 1, in ?
ImportError: no module named re

How could I make the classes from the jython jar "see" the modules python has available?

Answer

Blauohr picture Blauohr · Jan 27, 2009

You embed jython and you will use some Python-Modules somewere:

if you want to set the path (sys.path) in your Java-Code :

public void init() {
        interp = new PythonInterpreter(null, new PySystemState());

        PySystemState sys = Py.getSystemState();
        sys.path.append(new PyString(rootPath));
        sys.path.append(new PyString(modulesDir));
    }

Py is in org.python.core.

rootPath and modulesDir is where YOU want !

let rootPath point where you located the standard-jython-lib

Have a look at src/org/python/util/PyServlet.java in the Jython-Source-Code for example