Home > Articles > Programming > Java

Extending Jython

  • Print
  • + Share This
From the author of

Importing Jython Modules in Java

Importing a Jython module in Java usually uses the __builtin__.__import__ function. The __builtin__ module is actually a class in the org.python.core package that you must first import with:

import org.python.core.__builtin__;
// or more frequently org.python.core.*

The __builtin__.__import__ function has four signatures:

public static PyObject __import__(String name)

public static PyObject __import__(String name, PyObject globals)

public static PyObject __import__(String name, PyObject globals,
                 PyObject locals)

public static PyObject __import__(String name, PyObject globals,
                 PyObject locals,PyObject fromlist)

Importing the random module from within Java would be as follows:

PyObject module = __builtin__.__import__(random);
  • + Share This
  • 🔖 Save To Your Account