Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jython and python modules

Tags:

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?

like image 420
Geo Avatar asked Jan 22 '09 21:01

Geo


People also ask

Can Jython use Python libraries?

Jython does *not* support third-party Python libraries that use extensions written in C. This means that popular Python libraries like numpy, scipy and scikit-learn will not work in Jython (and, for this reason, will not work in Processing.py).

Is Jython the same as Python?

Jython and Python are two versions of the same language, used for different contexts. Jython is a Java implementation of Python, which, in a nutshell, means it's Python running on a Java Virtual Machine (JVM) environment. It writes like Python, but it can access the full potential of Java libraries.

How do I import a module into Jython?

You can do the easy_install for jython. Then next time you startup jython you will be able to import it. The official docs are here: jython.org/jythonbook/en/1.0/…

Is Jython still used?

Java. Jython was started by Jim Hugunin in 1997 as “JPython”, and has seen continued development since then. However, development of Jython is remains on the Python 2.7 line, a major release of the Python interpreter which, since the beginning of 2020, is no longer being maintained.


2 Answers

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

like image 196
Blauohr Avatar answered Sep 22 '22 07:09

Blauohr


According to the FAQ:

4.1 What parts of the Python library are supported?

The good news is that Jython now supports a large majority of the standard Python library. The bad news is that this has moved so rapidly, it's hard to keep the documentation up to date.

Built-in modules (e.g. those that are written in C for CPython) are a different story. These would have to be ported to Java, or implemented with a JNI bridge in order to be used by Jython. Some built-in modules have been ported to JPython, most notably cStringIO, cPickle, struct, and binascii. It is unlikely that JNI modules will be included in Jython proper though.

If you want to use a standard Python module, just try importing it. If that works, you're probably all set. You can also do a dir() on the modules to check the list of functions it implements.

If there is some standard Python module that you have a real need for that doesn't work with Jython yet, please send us mail.

In other words, you can directly use Python modules from Jython, unless you're trying to use built-in modules, in which case you're stuck with whatever has been ported to Jython.

like image 24
2 revs, 2 users 97% Avatar answered Sep 20 '22 07:09

2 revs, 2 users 97%