2

When I try to call file and its method using Jython it shows the following error, while my Numpy, Python and NLTK is correctly installed and it works properly if I directly run directly from the Python shell

File "C:\Python26\Lib\site-packages\numpy\core\__init__.py", line 5, in <module>
import multiarray
ImportError: No module named multiarray

The code that I am using is simple one:

PyInstance hello = ie.createClass("PreProcessing", "None");  
PyString str = new PyString("my name is abcd");
PyObject po = hello.invoke("preprocess", str);
System.out.println(po);

When I run only the file of python containing class PreProcessing and calling method preprocess it works fine, but with Jython it throws error.

Jython is unable to import all the libraries that have only compiled version kept in the folder not the class code itself. Like instead of multiarray.py it only has multiarray.pyd that is the compiled version so it is not getting detected in Jython.

Why is it showing this behaviour? How to resolve it?

Please help!

Carl F.
  • 6,718
  • 3
  • 28
  • 41
ninja123
  • 21
  • 1
  • 2

2 Answers2

2

I know this is an old thread, but I recently ran into this same problem and was able to solve it and I figure the solution should be here in case anyone in the future runs into it. Like said above, Jython cannot deal with numpy's pre-compiled c files, but within nltk, the use of numpy is very limited and it's fairly straightforward to rewrite the affected bits of code. That's what I did, and I'm sure it's not the most computationally effective solution, but it works. This code is found in nltk.metrics.Segmentation, and I will only paste relevant code, but it will still be a little much.

def _init_mat(nrows, ncols, ins_cost, del_cost):
    mat = [[4.97232652e-299 for x in xrange(ncols)] for x in xrange(nrows)]
    for x in range(0,ncols):       
        mat[0][x] = x * ins_cost
    for x in range(0, nrows):
        mat[x][0] = x * del_cost
    return mat

def _ghd_aux(mat, rowv, colv, ins_cost, del_cost, shift_cost_coeff):
    for i, rowi in enumerate(rowv):
        for j, colj in enumerate(colv):          
            shift_cost = shift_cost_coeff * abs(rowi - colj) + mat[i][j]
            if rowi == colj:
                # boundaries are at the same location, no transformation required
                tcost = mat[i][j]
            elif rowi > colj:
                # boundary match through a deletion
                tcost = del_cost + mat[i][j + 1]
            else:
                # boundary match through an insertion
                tcost = ins_cost + mat[i + 1][j]
            mat[i + 1][j + 1] = min(tcost, shift_cost)

Also at the end of ghd, change the return statement to

return mat[-1][-1]

I hope this helps someone! I don't know if there are other places where this is any issue, but this is the only one that I have encountered. If there are any other issues of this sort they can be solved in the same way(using a list of lists instead of a numpy array), again, you probably lose some efficiency, but it works.

Slater Victoroff
  • 21,376
  • 21
  • 85
  • 144
1

jython is Java. Parts of Numpy are implemented as c extensions to Python (.pyd files). Some parts are implemented as .py files, which will work just fine in Jython. However, they cannot function with out access to the C level code. Currently, there is noway to use numpy in jython. See:

Using NumPy and Cpython with Jython Or Is there a good NumPy clone for Jython?

For recent discussions on alternatives.

Community
  • 1
  • 1
Carl F.
  • 6,718
  • 3
  • 28
  • 41
  • But the issue I am facing is bit different. Numpy is working for everything except the one files which are already compiled ones. That is if the file is simply the code it works but not with the compiled class – ninja123 Aug 08 '11 at 03:30
  • That's correct. The compiled classes are compiled from C code. The classes with source code should work just fine, but they won't work independently. I'm still looking for a good substitute for numpy in Jython. Let me know if you find one! – Carl F. Aug 09 '11 at 00:27
  • I edited my answer to clarify that only parts of numpy are compiled. Pure Python components will work in Jython. Compiled parts will not. – Carl F. Aug 12 '11 at 02:05