Jython:导入错误:没有名为 multiarray 的模块

发布于 2024-11-28 00:12:25 字数 774 浏览 1 评论 0原文

当我尝试使用 Jython 调用 file 及其方法时,它显示以下错误,而我的 Numpy、Python 和 NLTK 已正确安装,并且如果我直接从 Python shell 直接运行,它可以正常工作

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

我正在使用的代码很简单:

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

当我仅运行包含类 PreProcessing 的 python 文件并调用方法 preprocess 时,它工作正常,但使用 Jython 时会抛出错误。

Jython 无法导入仅保存在文件夹中的编译版本而不是类代码本身的所有库。就像代替 multiarray.py 一样,它只有 multiarray.pyd ,它是编译版本,因此在 Jython 中不会检测到它。

为什么它会表现出这种行为?如何解决呢?

请帮忙!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

知足的幸福 2024-12-05 00:12:25

我知道这是一个旧线程,但我最近遇到了同样的问题并且能够解决它,我认为解决方案应该在这里,以防将来有人遇到它。如上所述,Jython 无法处理 numpy 的预编译 c 文件,但在 nltk 中,numpy 的使用非常有限,并且重写受影响的代码位相当简单。这就是我所做的,我确信这不是计算上最有效的解决方案,但它确实有效。这段代码是在nltk.metrics.Segmentation中找到的,我就只贴相关代码了,不过还是会有点多。

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)

另外,在 ghd 的末尾,将 return 语句更改为

return mat[-1][-1]

我希望这对某人有帮助!我不知道是否还有其他地方存在此问题,但这是我遇到的唯一一个。如果还有任何其他此类问题,可以用相同的方式解决(使用列表列表而不是 numpy 数组),同样,您可能会损失一些效率,但它是有效的。

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.

她说她爱他 2024-12-05 00:12:25

jython 是 Java。 Numpy 的某些部分被实现为 Python 的 c 扩展(.pyd 文件)。某些部分作为 .py 文件实现,在 Jython 中可以正常工作。然而,如果无法访问 C 级代码,它们就无法运行。目前,jython 中无法使用 numpy。请参阅:

将 NumPy 和 Cpython 与 Jython 结合使用
或者
Jython 是否有一个好的 NumPy 克隆?

对于最近的讨论关于替代方案。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文